The Montoya Herald, a weblog about Blueprint, jQuery, design, music and life, publishing on the web since September 2005. Written by Christian Montoya: developer, designer and entrepreneur.

The Montoya Herald — ChristianMontoya.com

Search

Supported By

For the love of all that is PHP, use constants

Posted on February 7.

Let me show you something cool:

define("API_KEY", "afb8c982");
 
// ...
 
echo("api key is: " . API_KEY);

If you haven't seen that before, you need to get acquainted. Why do I insist? Because PHP developers have a habit of using global variables like they are bad words. Then they end up with two problems:

Plus, when they sell their applications to someone I work for, I have to deal with the messiness and the difficulty of declaring globals when what I'm working with are just what the name implies: constants that never change.

So, please, seriously, use PHP constants! They are defined just as so, at the beginning of you code:

define("ADMIN_NAME", "Mr. Montoya");

You can only set them to static data, like strings or numbers; not the result of functions or anything, and the name has to start with a letter. Then you call them without any delimiters. I capitalize them as a habbit to maintain a convention. Hope that helps.

Get a Trackback link

8 Comments

  1. Phil on February 8, 2008

    Why does no-one else ever mention these! Looks incredibly useful. Thanks Christian!

  2. Sjaq on February 8, 2008

    Constants are great, but they are unchangeable. So I sometimes use my custom "Storage" class that saves variables, and using a Sigleton pattern it's global. What do you think about that, is it a good solution?

  3. Nick on February 8, 2008

    Good article :)

    When I first got into PHP I used a lot of global variables, but have since switched over to constants. I keep going back through some of my old code and can't believe how I used to right it. Embarrassing really.

  4. Christian Montoya on February 9, 2008

    Sjaq: Well, the point stands that they are for things that should never change. A storage object is OK, but I've never been too interested in using objects for data storage alone.

  5. Ron DeVera on February 12, 2008

    Ack, global abuse does need to go away. Constants are godsends for tweaking timer-based throttles, preset e-mail addresses, and other data that one doesn't want to keep restating as "global" in every single function.

  6. Connie on February 16, 2008

    I whished all these open source projects would store the database info etc. in Constants…

    I am proposing this since some time, but rarely nowhere I see that realised..

  7. Dean on February 17, 2008

    I use heredoc a lot and constants can't be used in heredoc to my knowledge which puts me off using them.

  8. Christian Montoya on February 18, 2008

    Dean, I used to have that concern, but now I just pass the constants to variables before my HEREDOCs and move right along.

Leave a comment

Use Markdown or basic HTML. For posting code, use Postable. Please keep comments respectful and on topic.