你也可以嘗試singleton模式,雖然在某種程度上它在OOP圈子中被壓垮了,但它通常被稱爲類的全局變量。
<?php
class Singleton {
// object instance
private static $instance;
// The protected construct prevents instantiating the class externally. The construct can be
// empty, or it can contain additional instructions...
protected function __construct() {
...
}
// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects. The methods can be empty, or
// can contain additional code (most probably generating error messages in response
// to attempts to call).
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}
//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
//One or more public methods that grant access to the Singleton object, and its private
//methods and properties via accessor methods.
public function GetSystemVar() {
...
}
}
//usage
Singleton::getInstance()->GetSystemVar();
?>
這個例子是從維基百科稍微修改,但你可以得到的想法。嘗試使用Google Singleton模式以獲取更多信息
這是否意味着常數>變量的名字呢? – atomicharri 2009-02-06 04:45:49
在這個問題中,SYSTEM是一個常量,而不是變量名。 – PolyThinker 2009-02-06 04:47:40
我不知道你在說什麼,但$ {SYSTEM}絕對不是和$ SYSTEM一樣...... – atomicharri 2009-02-06 04:56:03