Copy code The code is as follows:
/**
* Singleton pattern
*
* Ensures that a class has only one instance and provides a global access point to access it
*
*/
class Singleton
{
static private $_instance = null;
private function __construct()
{
}
static public function getInstance()
{
if(is_null(self::$_instance)) {
self::$_instance = new Singleton();
}
return self::$_instance;
}
public function display()
{
echo "it is a singlton class function";
}
}
// $obj = new Singleton(); // Statement Unable to succeed
$obj = Singleton::getInstance();
var_dump($obj);
$obj->display();
$obj1 = Singleton::getInstance( );
var_dump(($obj === $obj1));
http://www.bkjia.com/PHPjc/323802.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323802.htmlTechArticleCopy the code as follows: ?php /*** Singleton pattern * * Ensures that a class has only one instance and provides a global access point to access it **/ class Singleton { static private $_instance.. .