Jump to content

[SOLVED] Singleton constructor?


Andy-H

Recommended Posts

Only if the getInstance() method tells it to.

 

class myClass {

private static $_instance;

public static function getInstance() {
	if (!isset(self::$_instance) || is_null(self::$_instance)) {
		self::$_instance = new myClass();
	}

	return self::$_instance;
}	//	function getInstance()

function __construct() {
	echo 'called constructor<br />';
}	//	function __construct()

}	//	class myClass

myClass::getInstance();	//	Should display "called constructor"
myClass::getInstance();	//	Should not display "called constructor"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.