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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?