1

i have an application done in php and all configuration variables are loaded in a big $conf variable at the beginning of the script.

What is the better way to communicate this configuration variable to all other functions ?

make it a parameter of every function ? or use it with "global $conf;" statement in every function ?

is there a better way to do ?

Thanks

1
  • 1
    One approach is to split your configuration up into sections and have each component of your application load only the configuration section it needs access to. Alternatively you can store the info in your database and read it from there. Commented Apr 2, 2011 at 20:15

3 Answers 3

4

Use PHP constants.

For ponies sake, avoid using global variables at all costs :)

EDIT

Some explanations about "avoiding global variables at all costs" and possible alternatives:

Sign up to request clarification or add additional context in comments.

10 Comments

Avoidance at all costs. Gosh, I don't even want to imagine the cumbersome outcome.
@mario sorry for my english :)
Nah, it's not the english. You've run aground some pretty bad meme advise. -- That being said, constants are often (= not always) a good choice to harbour some config settings.
"You've run aground some pretty bad meme advise." - For what global variables should be used then ?
Application-wide config settings are pretty much one of the more useful (and not groundless quite common) use cases for them.
|
2

Make a configuration class that stores the options. Make it a singleton PHP Manual describes that here. This is just an alternative to global variables. It would allow you to define a method to load options from a file or a php array and store them in the class. Other classes can use the configuration object by getting the single instance and accessing the data.

I think this is better than a global variable as the other answer also says. But it still lets you define options as arrays, or even nested arrays if you want (and set up your class accordingly)

Comments

0

Your use of a single global-scoped variable $conf is perfectly fine. Many PHP applications do that. But there are drawbacks to combat.

In particular it's often more effort to write global $conf in each function where you want to access them. In that case I would recommend a simple global wrapper function instead:

function conf($key, $sub="") {
    global $conf;
    if (defined($key))
        { return constant($key); }
    elseif ($sub)
        { return $conf[$key][$sub]; }
    else
        { return $conf[$key]; }
}

This allows you to write conf("setting1") or conf("main", "opt3") whereever you need it. Still you can access the global $conf where that is more suitable. As extra bonus you can make this wrapper function more intelligent, by allowing it to query alternative settings etc. Also see how easy it is to also sneak in conf("CONSTANT") support.

Keeping this adds some flexibility in defining your configuration settings. Personally I use a similar approach, albeit with defining the array step-wise rather than at once:

$app_config["title"] = ...;
$app_config["editor.btns"] = ...;
define("RESTRICTED_MODE", true);

I'm preferring the array() approach, but transitioning to an ini-file for storage at a later point is not a problem. Also you can still make your config array read-only if the need arises. For that just define an:

class Read_Only_Array extends ArrayObject { function offsetSet() {} }
$conf = new ReadOnlyArray($conf);

So it's still accessible as array, but you easily established what others use cumbersome registries or syntactic workarounds for.


The "globals are evil" meme is completely baloney. It's parrotted on SO by cargo cult programmers with a desire for oversimplification and newcomers who glance over bold headlines without understanding the language semantics.

In your case, you just use a single $conf variable, and do not pollute the shared scope. When it is coherently accessed from the whole application, then it's not an issue. You should however strictly avoid to modify contents at runtime (use Read_Only_Array if need be). Create a secondary $app_var[] aray for that, and keep your config settings static.

12 Comments

i usually don't really like globals but as i have seen this $conf way of coding in several php sources, i think it can be done :) your function idea is nice to avoid declaring global $conf everywhere, thanks
Why not, but couldn't some function blah() { global $conf; ... $conf = 'blah'; ... } from a dumb developer or external library silently ruin all that ?
@FrostyZ: Possible, but not overly likely. And as said one can make it a read-only array if such a need factually arises.
@mario: assigning a read-only array to a variable, won't make that variable read-only. $conf = 'blah'; will simply erase the read-only array stored into $conf. About global variables: the only fact that they can be silently changed or "redeclared" (unlike constants for example), even if such a situation is not overly likely, is more than enough for me to call them evil. This awful reputation didn't pop out from nowhere. I'm telling this because I've experienced problems, not because I want to oversimplify or just repeat something without understanding.
@FrostyZ: I could make up a fictional example using runkit or classkit that "accidentially" screws your alternative Registry or Singleton aproach. Doesn't make it a problem in reality. Having stumbled upon problems with strpos() or being ticked off by abstract classes once years ago doesn't warrant declaring them evil either. That wording is still indicative of cargo cult programming. (It seems an extensive post is in order for this meme indoctrination..)
|

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.