重用MySQL连接PHP对象继承 [英] Reuse MySQL connection PHP object inheritance

查看:99
本文介绍了重用MySQL连接PHP对象继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个PHP脚本,以将主题从旧论坛站点迁移到新站点.

I'm writing a single PHP script to migrate topics from an old forums site to a new one.

  • 旧论坛站点使用数据库"old_forums"
  • 这些新论坛站点使用数据库"new_forums"
  • MySQL用户论坛"对两个数据库均具有所有特权(为了方便起见,我使用1个单个用户,但如果需要,使用2个不同的用户也不会有任何问题)

我将两个论坛都托管在同一主机-本地主机上

I have both forum hosted on the the same host - localhost

脚本具有以下结构

<?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();  
?>   

我了解我正在混合程序和此处是面向对象编程的PHP(OOPP). 我是面向对象的PHP的新手,但是(我有Java的经验,因此我很乐于接受一些有关制作纯OOPP的指南)

I understand that I'm mixing procedural & object-oriented programming PHP (OOPP) here. 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)

我想为OldForum和NewForum类使用1个单个MySQL连接.
我应该在哪里实例化mysqli对象?
例如在Forum类的构造函数中,或拥有一个新的mysqli对象作为Forum类的属性
这样我就可以创建一个新的Forum对象来启动MySQL连接

I would like to ultilise 1 single MySQL connection for both OldForum and NewForum class.
Where should I instantiate a mysqli object?
e.g. inside Forum class' constructor or have a new mysqli object as a property of class Forum
so that I would create a new Forum object to initiate a MySQL connection

$a_forum = new Forum();  

推荐答案

通过在引导文件中创建一次mysqli连接,然后将其传递给需要它的实例,该mysqli连接很容易在实例之间共享.

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, e.g.

$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.

对于您的导入和导出任务,我想知道为什么您完全使用PHP进行此操作.它显然是同一台数据库服务器,因此您可以在MySql实例中进行操作.如果您想用PHP做到这一点,我可能会做这样的事情:

As for your Import and Export Task, I wonder why you are doing this in PHP at all. It's apparently the same database server, so you could just do it inside your MySql instance. If you want to do it with PHP, I'd probably do something like this:

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 
    }
}

您可以将Import和Export方法提取到它们自己的类中,然后使用某种复合 命令模式,但这实际上取决于模块化程度您需要做到这一点.

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.

这篇关于重用MySQL连接PHP对象继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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