php - Destructor method for static instance variable? -


this class:

class mysqldb extends pdo{          private $_connection;         private static $_instance; //the single instance         private $_host = mysql_server_name;         private $_username = mysql_server_username;         private $_password = mysql_server_password;         private $_database = mysql_server_dbname;         /*         instance of mysqldb         @return instance         */         public static function getinstance() {                 if(!self::$_instance) { // if no instance make 1                         self::$_instance = new self();                 }                 return self::$_instance;         }          // pdo mysql connection         public function getconnection() {                 return $this->_connection;         }      public function __construct(){         try {             $this->_connection = new pdo("mysql:host={$this->_host};dbname={$this->_database};charset=utf8", $this->_username, $this->_password);             $this->_connection->setattribute(pdo::attr_errmode, pdo::errmode_exception);             $this->_connection->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_assoc);         }         catch(pdoexception $e) {             throw new exception($e->getmessage());         }     }      public function __destruct(){         // unset($this->_connection);         self::$_instance = null;     }  } 

this in main:

$db = mysqldb::getinstance();  $db->__destruct();  print_r($db).'<br /><br />'; 

this output:

mysqldb object ( [_host:mysqldb:private] => localhost [_username:mysqldb:private] => username [_password:mysqldb:private] => [_database:mysqldb:private] => dbname ) 

my question: why output? if i'm calling __destruct() method above, shouldn't empty output - since i'm nulling instance?

or maybe should use in main:

$db = null; print_r($db).'<br /><br />'; 

how make sure i've closed connection , killed object?

no, $db->__destruct() nothing more calling __destruct() method. not magically unset variable.

destructors work other way around: when object gets destroyed garbage collector after no variable references anymore, then destructor called automatically.

what want is:

unset($db); 

but note destructor not guaranteed called in moment. first, object must not assigned other variable , second, garbage collector cleans unreferenced objects executed periodically , not after each command.

in case, object still referenced mysqldb::$_instance. if change current __destruct method unsets self::$_instance static method resetinstance(), can use:

mysqldb::resetinstance(); unset($db); 

then both references gone , desctructor called (again, if there no other references on object).

more information on topic of reference counting can found in garbage collector manual: http://php.net/manual/de/features.gc.refcounting-basics.php


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -