简体   繁体   English

在PHP OOP中继承数据库连接

[英]Inheriting database connection in PHP OOP

My abstract class Database is made for database interaction and any child of this class (UserDatabase, ActionDatabase, EventDatabase) inherits its database connection which is defined as static . 我的抽象类数据库是数据库交互和这个类的任何子女作出(UserDatabase,ActionDatabase,EventDatabase)继承其定义为静态数据库连接

abstract class Database {  
    public static $connection;   
    public function __construct( ) {      
        ...  
        $this->connection = mysql_connect( $host, $username, $password );      
    }  
}     
class UserDatabase extends Database {  
    public function verify( ) {
        if ( $this->connection == parent::$connection ) {
            print "true";  
        } else {  
            print "false";  
            print "this:" . $this->connection . " parent:" . parent::$connection;  
        }  
    }    
}  
$instance = new UserDatabase( );  
$instance->verify( );  
// this prints false, as parent::$connection is empty

Does that mean, that my database connection is only set up and stored in memory once and passed on to subclasses as reference without being replicated for each instance? 这是否意味着我的数据库连接仅建立一次并存储在内存中然后传递给子类作为引用,而不为每个实例复制?

Is this how you would implement you OOP-correct database interface? 这是实现OOP正确的数据库接口的方式吗?

Ignoring your code for a moment and looking only at your description of how this scheme should work... Yes, the database connection would only be set up once for each PHP script and would not be duplicated for each instance. 暂时忽略您的代码,仅查看您对该方案如何工作的描述...是的,数据库连接将只为每个PHP脚本设置一次,并且不会为每个实例重复。

Whether this is a good idea or not depends on what your subclasses are. 这是否一个好主意取决于子类是什么。 Each class should have one responsibility. 每个班级应负一个责任。 As long as the subclasses' only responsibility is DB interaction, you're probably fine. 只要子类的唯一职责是数据库交互,就可以了。 (Look at the Repository Pattern for an explanation of how this is commonly done.) If your User class extends Database so that it can store itself, you've crossed a line and are writing classes that will have too many dependencies and responsibilities. (请查看存储库模式以获取有关如何完成此操作的解释。)如果您的User类扩展了Database以使其可以存储自身,则您已经越界了,正在编写将具有过多依赖关系和职责的类。

As for your code... Almost every line of your code is wrong. 至于您的代码...几乎每一行代码都是错误的。 You cannot initialize class variables to the results of a function. 您不能将类变量初始化为函数的结果。 Your SQL is wrong. 您的SQL错误。 The line $connection ? "connected" : "not connected"; $connection ? "connected" : "not connected";$connection ? "connected" : "not connected"; $connection ? "connected" : "not connected"; does nothing. 什么也没做。 Almost none of that should be done in a constructor. 几乎所有这些都不应该在构造函数中完成。 Etc. 等等。

That pattern is OK if you're doing nothing else with those classes but database interactions. 如果您除了数据库交互之外对这些类不做任何其他事情,则该模式是可以的。

Otherwise you may be better off with a Database class that's created once that you can "getInstance()" of each time. 否则,您最好使用一次创建的Database类,每次可以“ getInstance()”进行创建。 Singleton Design Pattern. 单例设计模式。

http://www.tonymarston.net/php-mysql/singleton.html http://www.tonymarston.net/php-mysql/singleton.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM