我创建一个PHP单例类:与Singleton类的实例处理
<?php
class DataManager
{
private static $dm;
// The singleton method
public static function singleton()
{
if (!isset(self::$dm)) {
$c = __CLASS__;
self::$dm = new $c;
}
return self::$dm;
}
// Prevent users to clone the instance
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function test(){
print('testsingle');
echo 'testsingle2';
}
function __get($prop) {
return $this->$prop;
}
function __set($prop, $val) {
$this->$prop = $val;
}
}
?>
现在,当我尝试在我的index.php使用这个类:
<?php
include('Account/DataManager.php');
echo 'test';
$dm = DataManager::singleton();
$dm->test();
echo 'testend';
?>
唯一的回声我得到的,是'test',单例类中的函数test()从来没有被调用过。此外,index.php末尾的“testend”从未被调用过。
在我的单身课程中是否有错误?
无法重现此...示例代码是在我的机器上工作。 – 2011-04-28 15:52:01
如果您只能进行'echo'测试“;'您可能需要打开错误报告。 – 2011-04-28 15:56:16
我认为处理单身人士课程的最好方法是不要处理他们。 ;) – netcoder 2011-04-28 16:39:25