class test{
static private $_instance;
public static function getInstance()
{
if(empty(self::$_instance))
{
self::$_instance =&$this;
}
return self::$_instance;
}
public function my()
{
echo 'mymy';
}
}
//$obj1,$obj2,$obj3相等
$obj1=test::getInstance();
$obj2=test::getInstance();
$obj3=test::getInstance();
$obj3->my();
弄不懂这里的&$this是什么意思。照理说$this表示对象的引用,再取地址表示个test对象在内存中的地址,再赋值给静态变量$_instance,这样$obj1,$obj2,$obj3应该都是一样的test对象了,应该可以调用my方法了,但实际报错。请大神指教下。
你的

self::$_instance =&$this;
这里是不能实例化当前类的 所以打印出的你代码的$obj3是Null正确的写法应该是这样的