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

6
  • 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. Commented Jan 3, 2013 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. Commented Jan 3, 2013 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 Commented Jan 3, 2013 at 18:01
  • @Neal, that's pretty much exactly what he acknowledged to now know? Commented Jan 3, 2013 at 18:03
  • @h2ooooooo ahhh I thought the OP said "could" Commented Jan 3, 2013 at 18:04

1 Answer 1

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;
       //...
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I found a good tutorial on PHP Classes and I believe you're right - exactly what I needed; Thanks! EDIT: Will accept in 7 :)
Also, most of the time private variables are not needed as much as protected variables are. You need to support future class additions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.