PHP global var for everything?

Possible duplicate:
Sample registry design ... good or bad?

I am working on a script game that uses PHP and approaches it in the same way as I create many of my sites. I have the habit of declaring one variable, which is stdClass, which contains many other variables that are important for execution. Then, if I ever need something inside the function (if it is not passed in the parameters), I just use the 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 used one of the above variables in a function, I would approach it like this.

 public function run() { global $ar; //do something with the variable.. } 

Anyone advise not to do this? Does a single variable use and include many functions that should be avoided? I know that it is advisable to create functions that work only with the specified parameters, but I ask about this in terms of PHP performance and not programming clarity. Thanks!

+4
source share
1 answer

Data elements that are combined to represent an object must be objects. If you have material that is not connected, do not use it in the same object.

If you need a global scope (and you usually don't), you can use $GLOBALS . However, I suggest you follow the principles of OOP for most projects. At a minimum, use functional parameters.

If you had a library of functions like run() for which these variables are needed (or even just run() !), There is a utility class in which these variables are members of this class.

+1
source

All Articles