NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP 5 singleton/destructor question

David Mintz dmintz at davidmintz.org
Thu Mar 31 16:27:26 EST 2005


I can't seem to get my destructor to run and I wonder if somebody can help
me see what I am doing wrong. The goal is simply to have a single instance
of this db connection object and have its close() method called
automatically before the script exits.

class MyDatabase extends mysqli  {

	private static $db = false;

	private function __construct() {

		printf("constructing %s...<br />",__CLASS__);
		parent::__construct("host", "user", "password",
"database");
	}

	public static function getInstance() {

		if (self::$db === false) {
			self::$db = new MyDatabase();
		}
		return self::$db;
	}

	function __destruct() { echo "closing...";  self::$db->close();}

}

$db = MyDatabase::getInstance();
print $db->host_info;
---

OUTPUT
constructing MyDatabase...
localhost via TCP/IP
---


---
David Mintz
http://davidmintz.org/



More information about the talk mailing list