OO PHP中可重用结构的说明 [英] Explanation of re-usable structures in OO PHP

查看:155
本文介绍了OO PHP中可重用结构的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以为我解释可重复使用的结构吗?
我正在做一些db对象在php,但被告知我使用太多的处理从计算机,因为我做的东西复杂与以下对象:

Can someone please explain "re-usable structures" for me? I was working on making some db objects in php, but was told I was using too much processing from the computer cause I made stuff to complicated with the below objects:

我的数据库对象:

$db = new Database;
$db->db_connect();

$post_content = new DbSelect;
$post_content->select('id', 'title', 'firstName', 'created', 'catName', 'tagName');
$post_content->from('content');   
$post_content->join('inner');
$post_content->on('category','cat_id','id');
$post_content->where('id','1');
$post_content->order('created');

$db->db_close();

正常PHP:

mysql_connect();
mysql_db_select();

$query = 'SELECT id, title, s_name, created, cat_name, tag_name
                FROM content
                JOIN INNER category, cat_id, id
                WHERE id=1
                ORDER created';

mysql_close();

所以重申我的问题:
1.可重用结构的快速解释?
2.为什么第一个方法使用对象错误?

So to reiterate my questions: 1. A quick explanation of re-usable structures? 2. why is the first method using objects "wrong"?

请注意:
我将googling这个以及希望反馈
我知道有一个工具像Zend和其他有很多db对象内置在他们,但我正在尝试一个DIY方法

please note: I'll be googling this as well as hoping for feedback I know there a "tools" like Zend and other's that have plenty of db objects built into them, but I'm trying a DIY approach

推荐答案

我不知道从哪里开始。面向对象的设计不是一个简单的主题,并且有很多方法可以出错。

I'm not sure where to start on this one. Object Oriented design is not a trivial subject, and there are many ways it can go wrong.

基本上,你想尝试在应用程序中创建逻辑独立对象,您可以将其交换为具有相同接口的其他模块,或在将来的项目中重用它们。在你的数据库例子中,看看PEAR :: MDB2。 PEAR :: MDB2将数据库驱动程序从应用程序中抽象出来,这样就不必担心要使用哪个特定数据库。今天,你可能使用MySQL来运行你的网站。明天,您可能切换到Postgresql。理想情况下,如果你使用适当的OO设计,你不需要改变任何代码,使其工作。你只需要换出另一个数据库层。 (Pear :: MDB2使这很简单,因为更改你的db连接字符串)

Essentially, you want to try to make logical indepedent objects in your application such that you can swap them out for other modules with the same interface, or reuse them in future projects. In your database example, look at PEAR::MDB2. PEAR::MDB2 abstracts the database drivers away from your application so that you don't need to worry about which specific database you're using. Today, you might be using MySQL to run your site. Tomorrow, you might switch to Postgresql. Ideally, if you use a proper OO design, you shoudn't need to change any of your code to make it work. You only need to swap out the database layer for another. (Pear::MDB2 makes this as simple as changing your db connect string)

我建议阅读由Steve McConnell的Code Complete。关于Classes有一整章。虽然示例主要是C ++,但这些概念可以应用于任何编程语言,包括PHP。

May I suggest reading Code Complete by Steve McConnell. There's a whole chapter on Classes. While the examples are primarily C++, the concepts can be applied to any programming language, including PHP.

这篇关于OO PHP中可重用结构的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆