gpt4 book ai didi

PHP 全局变量的一切?

转载 作者:可可西里 更新时间:2023-10-31 23:41:55 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Registry design pattern…good or bad?

我正在编写一个使用 PHP 的游戏脚本,并以与我构建许多网站的方式类似的方式处理它。我有一个习惯,即声明一个单独的变量,它是一个 stdClass,其中包含许多对执行很重要的其他变量。然后,如果我在函数内部需要某些东西(如果它没有传入参数),我只需使用全局 $variable,在本例中为 $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 性能而不是编程清晰度。谢谢!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com