我創建一個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