Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP global var for everything?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/1151341/registry-design-pattern-good-or-bad">Registry design pattern…good or bad?</a> </p> </blockquote> <p>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.</p> <pre><code>$ar = new stdClass; $ar-&gt;i = new stdClass; $ar-&gt;s = new stdClass; $ar-&gt;i-&gt;root = "/home/user"; /* Directory where log files are to be stored. */ $ar-&gt;i-&gt;logs = "{$ar-&gt;i-&gt;root}/logs"; /* Directory where the banlist is. */ $ar-&gt;i-&gt;bl = "{$ar-&gt;i-&gt;root}/customize/settings/bannedaccounts.txt"; /* Directory where the silencelist is. */ $ar-&gt;i-&gt;sl = "{$ar-&gt;i-&gt;root}/customize/settings/silencedaccounts.txt"; /* How many points should be awarded for 1st place, 2nd place... etc. */ $ar-&gt;s-&gt;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); </code></pre> <p>So if I were to use one of the above variables in a function, I would approach it like this.</p> <pre><code>public function run() { global $ar; //do something with the variable.. } </code></pre> <p>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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload