Possible Duplicate:
Registry design pattern…good or bad?
I am working on a game script that uses PHP and am approaching it in a similar way to how I build many of my websites. I have a habbit of declaring a single variable that is a stdClass holding many other variables that are important to execution. Then if I ever need something inside a function (if its not passed in the parameters), I simply use global $variable, in this case $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);
So if I were to use one of the above variables in a function, I would approach it like this.
public function run() {
global $ar;
//do something with the variable..
}
Would anyone advise not to do this? Is using a single variable and including it in a lot of functions a bad practice to avoid? I know that it is advisable to create functions that work with just the parameters given, but I am asking this in regard to PHP performance and not programming clarity. Thanks!