dqm4977 2013-12-11 18:17
浏览 35
已采纳

创建PHP对象时重用数据库连接

In PHP, I have two classes: Database and Item.

Database contains the connection properties and methods. For example, Database::Query can be used to pass a query string, etc.

Item is a generic item identifier. It's built by passing it an itemID which is then used to query the database for the rest of the item information.

In this case, what's the best practice for creating Item objects if they require database access? Is it normal to create each one using this syntax:

$item = new Item(12345, $db);

Or is it better, acceptible, or possible to create the Database object and have it used for each Item created in the application, such that the call could become:

$item = new Item(12345);

The second seems a lot cleaner (and can be expanded so that similar types of objects don't also need that $db addon), but I'm looking for suggestions from those who have more experience at this than I do! Thanks!

  • 写回答

3条回答 默认 最新

  • doupai6875 2013-12-11 18:46
    关注

    I would suggest that most seasoned developers would lean toward the approach of dependency injection as demonstrated in your first example.

    Why?

    Well largely because this allows you to decouple the class to which the dependency is being injected from the dependency's implementation.

    So consider this dependency injection example:

    Class some_class {
        protected $db;
        __construct($db) {
            if($db instanceof some_db_class === false) {
                throw new Exception('Improper parameter passed.');
            }
            $this->db = $db;
        } 
    
    }
    

    Here you could pass any type of object so long as it was an instance of some_db_class it could be a subclass of that object that implements the same methods used by this class. That doesn't matter to this class as long as the methods are implemented (you of course could also check that a passed object implements a specific interface in addition to or in lieu of checking its instance type).

    This means that, for example, you can pass a mock DB object for testing or something like that. The class doesn't care as long as the methods are implemented.

    Now consider the singleton approach (or similar instantiation of DB from with the class):

    Class some_class {
        protected $db;
        __construct() {
            $this->db = some_db_class::get_instance();
        } 
    }
    

    Here you have tightly coupled your class to a specific database class. If you wanted to test this class with a mock DB implementation it becomes very painful in that you need to modify the class to do so.

    I won't even get into discussion of using global as that is just poor practice and should not be considered at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!