Reusing MySQL connections Inheriting PHP objects

I am writing one PHP script to transfer topics from the old forum site to the new one.

  • The old forum site uses the database "old_forums"
  • Thew new forum uses the database "new_forums"
  • MySQL user forums have all privileges for both databases (I use 1 of one user for convenience, but I will not have problems using 2 different users, if necessary).

I have a forum hosted on the same host - localhost

script I have the following structure

<?php class Forum { //constants const HOST = "localhost"; const DB_USER = "forums"; const DB_PASS = "forums"; ... //properties eg 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 here I am mixing procedural and object-oriented programming of PHP (OOPP). I am new to object oriented PHP, but (I have experience with Java, so I am very open to some guidance to make this pure OOPP)

I would like to improve one single MySQL connection for the OldForum and NewForum classes.
Where should I instantiate the mysqli object?
for example, inside the constructor of the Forum class, or have a new mysqli object as a property of the Forum class
so I would create a new Forum object to initiate a connection to MySQL

 $a_forum = new Forum(); 
+4
source share
2 answers

A mysqli connection is simple enough to share between instances, creating it once in your bootstrap file, and then passing it to the instances it needs, for example.

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

This will effectively limit the connection to one, and you do not need to resort to global objects inside your objects. This is called Injection Dependency Injection and should be your preferred way to assign dependencies to objects. This makes dependencies clear and easily replaceable, and thus helps change, test, and maintain.

As for your import and export task, I wonder why you do this in PHP at all. This is apparently the same database server, so you can just do it in your MySql instance. If you want to do this with PHP, I would 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 } } 

You can extract the import and export methods into your own classes and then use some sort of Composite Command Pattern , but it really depends on how modular you need it.

+2
source

Another approach would be to create several VIEW in the new_forums database pointing to the old_forum database. In this case, all "views" can have, for example, the prefix "old_"? Something like this:

 CREATE VIEW old_tbl_name AS SELECT * FROM old_forum.tbl_name 

In such a situation, you only have one connection to one database.

+1
source

Source: https://habr.com/ru/post/1444564/


All Articles