4

I've got a handful of variables that I need access to in more functions than I initially thought.

What would be the best way of including them in my growing list of functions?

I've got something like

$site = 'a';
$admin = 'b';
$dir = 'c';
$ma_plug = 'd';
$base = 'e';
//Etc.

I need almost all of those in a large handful of my functions. I was initially doing

function a(){
    global  $site, $admin, $dir, $ma_plug, $base;
    //Write the A function
}
function b(){
    global  $site, $admin, $dir, $ma_plug, $base;
    //Write the B function
}

That was great, until I realized I've got more functions to write than I initially thought.

What would be the best way to get all those variables into the scope of (almost) each function? Like I've done? Or something like using Constants? I'd like to avoid using session() if at all possible

Xhynk
  • 13,513
  • 8
  • 32
  • 69
  • 3
    Classes and `$this->`. Constants are good, but can not be edited afterwards. If you're adding a variable which is constant (does not change), and **has** to be available in the global scope, use constants. Please use constants WRITTEN_LIKE_THIS for readability of your own code, as they're much easier to spot. – h2ooooooo Jan 03 '13 at 17:59
  • 5
    Please note that "bad practice" is not the same as "bad question". Please stop downvoting questions because you don't like the code, that is not what question votes are for. If the people asking questions knew how to write the code they wouldn't be asking the question. – DaveRandom Jan 03 '13 at 18:01
  • Thanks for that - I didn't realize they couldn't be edited after definition. That could be an issue with some of the variables I've got – Xhynk Jan 03 '13 at 18:01
  • @Neal, that's pretty much exactly what he acknowledged to now know? – h2ooooooo Jan 03 '13 at 18:03
  • @h2ooooooo ahhh I thought the OP said "could" – Naftali Jan 03 '13 at 18:04
  • possible duplicate of [Safe alternatives to PHP Globals (Good Coding Practices)](http://stackoverflow.com/questions/7290993/safe-alternatives-to-php-globals-good-coding-practices) or http://stackoverflow.com/questions/5166087/php-global-in-functions or http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why or http://stackoverflow.com/questions/12445972/stop-using-global-in-php – Mike B Jan 03 '13 at 18:29

1 Answers1

6

If these functions are the only ones using these variables, it might better to make a class in which these variables are local to.

That way you do not pollute the global namespace and have the need to use the global keywords in every function.

For example:

class MySite {
    private $site;
    private $admin;
    private $dir;
    private $ma_plug;
    private $base;

    function __construct($site, $admin, $dir, $ma, $base) {
       $this->site = $site;
       $this->admin = $admin;
       //...
    }
}
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • I found a good tutorial on PHP Classes and I believe you're right - exactly what I needed; Thanks! **EDIT**: Will accept in 7 :) – Xhynk Jan 03 '13 at 18:06
  • 1
    Also, most of the time `private` variables are not needed as much as `protected` variables are. You need to support future class additions. – Xeoncross Jan 03 '13 at 18:10