I have a class that allows a PDO connection which sets itself up. When I want to use the connection I can use:
$db = $factory->create('db');
However, I wanted to just be able to do:
global $db;
Anytime I want to use the database.
Would this work?
$db = function(){
$con = $factory->create('db');
return $con;
};
global $db;
This way, I can close the connection and then open it again at any point. Example:
global $db;
$db->close();
// Re-open
global $db;
Or how could I possibly do this? References would be so appreciated as I have searched a lot.
Thanks in advance.
$dbglobal then whats the point for the factory pattern ?? just curiousglobal $db;every time I want to use the database and just be able to close it and re-open it the way it says. Can anyone link me any references to achieve this?$db->close()does, the connection is not going to magically reopen when you change the variable scope, which is all the global keyword is doing.$db->close()probably isn't doing anything. You should focus your learning on the proper usage and separation of variable scopes, not trying to adapt the code to how MyBB works.