I have the following code:
//init X (DB initialization with credentials)
$x = MySqlConnector::getMySql();
//I destroy $x
unset($x);
$x = null;
//I try to re-initialize the database, but it is already initialized
//as evident from my logs
$x = MySqlConnector::getMySql();
Relevant function:
public static function getMySql()
{
if (null === static::$instance)
{
include 'include/config.php';
static::$instance = new MySql(DBHOST, DBUSER, DBPASS);
}
return static::$instance;
}
That tells me that even after I kill off the variable that was holding the initialized object, somehow MySqlConnector
stayed in memory.
How? I don't think it works with any other non-static class.
Static properties exist in the global scope, and are not associated to any particular instance.
You may be unsetting $x
, but MySqlConnector::$instance
stays defined.
Typically, in this kind of scenario, $instance
will be a private static
, so you wont be able to access the property directly, only through accesor methods, hence guaranteeing that only the Singleton class will have access to modify the property, and you wont be changing/setting/unsetting it but through properly defined methods, if they exist.
More info in the manual.