我有這樣的代碼:如何使對象全局可訪問?
class IC_Core {
/**
* Database
* @var IC_Database
*/
public static $db = NULL;
/**
* Core
* @var IC_Core
*/
protected static $_instance = NULL;
private function __construct() {
}
public static function getInstance() {
if (! is_object(self::$_instance)) {
self::$_instance = new self();
self::initialize(self::$_instance);
}
return self::$_instance;
}
private static function initialize(IC_Core $IC_Core) {
self::$db = new IC_Database($IC_Core);
}
}
,但是當我想訪問IC_Database:
$IC = IC_Core::getInstance();
$IC->db->add() // it says that its not an object.
我認爲問題出在自身:: $ DB =新IC_Database($ IC_Core);
但我不知道如何使它工作。
有人可以給我一隻手=)
謝謝!
感謝它的工作! – 2010-05-08 18:21:11
使用非靜態'$ db'的靜態'$ _instance'似乎沒什麼意義。 – Tgr 2010-05-08 18:52:20
@Tgr:'$ db'是實例本地的,但只有其中一個。它以相同的方式工作。 – 2010-05-08 19:09:42