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(); $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() {
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.
source share