public static function & func_GetInstance()//Singleton
{
static $obj_instance = null;
if(empty($obj_instance))
{
echo "执行我了";
$obj_instance = new cls_BoxCore();
}
return $obj_instance;
}这是使类实例化的代码(SINGLETON模式)
创建实例时这样用
$obj_Box = & cls_BoxCore::func_GetInstance();
但是我在创建过一次以后,再次创建如上的引用方法,却发现不是返回静态保存的$obj_instance对象,而是重新又建了一个,然后又因为类内定义了常量,所以又报错说常量重复定义了。
我想知道为什么没如愿返回保存的静态值呢?
class cls_BoxCore
{
public static function & func_GetInstance()//Singleton
{
static $obj_instance = null;
if(empty($obj_instance))
{
echo "执行我了";
$obj_instance = new cls_BoxCore();
}
return $obj_instance;
}private function __construct()
{
$b = & cls_BoxCore::func_GetInstance();
}
}是这种原因,可是我想知道这样为什么会出错呢?