简体   繁体   English

重用MySQL连接PHP对象继承

[英]Reuse MySQL connection PHP object inheritance

I'm writing a single PHP script to migrate topics from an old forums site to a new one. 我正在编写一个PHP脚本,用于将主题从旧论坛站点迁移到新论坛站点。

  • The old forums site use database "old_forums" 旧论坛网站使用数据库“old_forums”
  • Thew new forums site use database "new_forums" 新的论坛网站使用数据库“new_forums”
  • MySQL user "forums" has all privileges to both databases (I'm using 1 single user for convenience but I would not have any problems using 2 different users if required) MySQL用户“论坛”拥有两个数据库的所有权限(为方便起见,我使用的是单个用户,但如果需要,我会使用2个不同的用户时遇到任何问题)

I have both forum hosted on the the same host - localhost 我有两个论坛托管在同一主机 - localhost

The script I have the following structure 脚本我有以下结构

<?php  
class Forum {  
   //constants  
   const HOST = "localhost";  
   const DB_USER = "forums";  
   const DB_PASS = "forums";  
   ...  
   //properties e.g. topic title, topic content
}
class OldForum extends Forum {  
   const DB_NAME_OLD = "old_forums";  
   public static function exportTopics() {  
       //some code
   }  
}
class NewForum extends Forum {
   const DB_NAME_NEW = "new_forums";
   public static function importTopics() {  
       //some code
   }
}  
OldForum::exportTopics();  
NewForum::importTopics();  
?>   

I understand that I'm mixing procedural & object-oriented programming PHP (OOPP) here. 我知道我在这里混合了程序和面向对象编程PHP(OOPP)。 I'm new to object-oriented PHP but (I have experience with Java so I'm very open to some guide to make this pure OOPP) 我是面向对象PHP的新手,但是(我有Java经验,所以我非常愿意接受一些指导来制作这个纯粹的OOPP)

I would like to ultilise 1 single MySQL connection for both OldForum and NewForum class. 我想为OldForum和NewForum类提供1个单独的MySQL连接。
Where should I instantiate a mysqli object? 我应该在哪里实例化mysqli对象?
eg inside Forum class' constructor or have a new mysqli object as a property of class Forum 例如,在Forum类的构造函数中,或者将一个新的mysqli对象作为类Forum的属性
so that I would create a new Forum object to initiate a MySQL connection 这样我就可以创建一个新的Forum对象来启动MySQL连接

$a_forum = new Forum();  

The mysqli connection is easy enough to share between instances by creating it once in your bootstrap file and then passing it to instances that need it, eg 通过在引导程序文件中创建一次,然后将其传递给需要它的实例,mysqli连接很容易在实例之间共享,例如

$mysqli = new mysqli(/* connection params */);
$someClassUsingMySqli = new SomeClassUsingMySqli($mysqli);
$anotherClassUsingMySqli= new AnotherClassUsingMySqli($mysqli);

That will effectively limit the connection to one and you dont need to resort to globals inside your objects. 这将有效地限制连接到一个,你不需要求助于对象内的全局变量。 This is called Dependency Injection and should be your prefered way of assigning dependencies to objects. 这称为依赖注入,应该是您为对象分配依赖关系的首选方式。 It makes dependencies explicit and easy to swap out and thus benefits change, testing and maintenance. 它使依赖关系明确且易于更换,从而有利于变更,测试和维护。

As for your Import and Export Task, I wonder why you are doing this in PHP at all. 至于你的导入和导出任务,我想知道你为什么要在PHP中这样做。 It's apparently the same database server, so you could just do it inside your MySql instance. 它显然是相同的数据库服务器,因此您可以在MySql实例中执行此操作。 If you want to do it with PHP, I'd probably do something like this: 如果你想用PHP做,我可能会做这样的事情:

class MigrateForum
{
    private $dbConnector;

    public function __construct(DBConnector $dbConnector)
    {
        $this->dbConnector = $dbConnector;
    }

    public function migrate()
    {
        // orchestrate the migration (consider transactions)
        $this->exportOldForum();
        $this->importNewForum();
    }

    private function exportOldForum()
    {
        // code to export old_database_name.table_name 
    }

    private function importOldForum()
    {
        // code to import new_database_name.table_name 
    }
}

You could extract the Import and Export methods into their own Classes and then use some sort of Composite Command Pattern , but that really depends on how modular you need this to be. 您可以将导入和导出方法提取到他们自己的类中,然后使用某种复合 命令模式 ,但这实际上取决于您需要的模块化程度。

Different approach would be to create some VIEW in the new_forums database pointing to the old_forum database. 不同的方法是在new_forums数据库中创建指向old_forum数据库的一些VIEW In this case, all the "views" could have, for example, "old_" prefix? 在这种情况下,所有“视图”可能都有,例如,“old_”前缀? Something similar to this: 与此类似的东西:

CREATE VIEW old_tbl_name AS
     SELECT *
       FROM old_forum.tbl_name

In such situation, you only have one connection to one database. 在这种情况下,您只有一个连接到一个数据库。

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

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