我正在使用PHP遊戲腳本,並正在接近它以類似的方式如何,我建我的許多網站。我有一個聲明單個變量的習慣,這個變量是一個stdClass,它包含許多對執行很重要的其他變量。然後,如果我需要一個函數內部的東西(如果它沒有傳入參數),我只需使用全局$變量,在這種情況下$ ar。
$ar = new stdClass;
$ar->i = new stdClass;
$ar->s = new stdClass;
$ar->i->root = "/home/user";
/* Directory where log files are to be stored. */
$ar->i->logs = "{$ar->i->root}/logs";
/* Directory where the banlist is. */
$ar->i->bl = "{$ar->i->root}/customize/settings/bannedaccounts.txt";
/* Directory where the silencelist is. */
$ar->i->sl = "{$ar->i->root}/customize/settings/silencedaccounts.txt";
/* How many points should be awarded for 1st place, 2nd place... etc. */
$ar->s->points = array(10, 7, 5, 4, 3, 2, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1);
所以,如果我要在函數中使用上述變量之一,我會這樣處理它。
public function run() {
global $ar;
//do something with the variable..
}
有人會建議不要這樣做嗎?使用單個變量並將其包含在很多函數中是一種不好的做法以避免?我知道創建一些只能使用參數的函數是可取的,但我問的是PHP的性能,而不是編程的清晰度。謝謝!
這是[Registry Pattern](http://martinfowler.com/eaaCatalog/registry.html)。總是有更好的方法 – Phil