class test
{
private static $instance;
private function __construct()
{
echo 2;
}
public static function getInstance()
{
if( !( self::$instance instanceof self ) )
{
echo 1;
self::$instance =new self();
}
return self::$instance;
}
private function __clone()
{
}
}test::getInstance(); //12
self::$instance =new self() 这里实例存不进静态私有变量里面啊,怎么实现单例?还是我哪里写错了?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
1、单例类只能有一个实例。
2、单例类必须自己创建自己的唯一实例。
3、单例类必须给所有其他对象提供这一实例。