Aaron Saray

open source programmer,
web developer

entrepreneur, author
and musician

My Blog

contains PHP, Web and business/entrepreneurial related content. Please join in the conversation!

Don't forget about Class Constants!

Constants can be great. They can stand for things like web services keys, integers, flags, etc. Basically, anything that you aren’t going to be changing in your script – and most likely things that don’t change much outside of the script either. However, I’ve seen people use them in the global name space far too many times. A great alternative is the class constant. Lets check out some examples:

The Bad Code

First off, I can’t tell you how many times I’ve seen code like this:

1
2
3
4
5
6
7
8
9
10
11
12
define('MYCLASS_FLAG_ON', '1');
define('MYCLASS_FLAG_OFF', '0');

class MYCLASS
{
  public function __construct($var)
  {
    if ($var == MYCLASS_FLAG_ON) {
    print 'it is on';
    }
  }
}

Basically, you’ll see that they are being smart and using constants for some specific flags. However, they’re cluttering the global namespace with constants that probably won’t be used outside of the class (even if they ARE, we have a way to work around that.)

Now, lets take a look at the alternative.

The Good Code

Lets use the class constant.

1
2
3
4
5
6
7
8
9
10
11
12
class MYCLASS
{
  const FLAG_ON = 1;
  const FLAG_OFF = 0;

  public function __construct($var)
  {
    if ($var == self::FLAG_ON) {
      print 'it is on';
    }
  }
}

Now, you’ll see there is no congestion in the global name space.

But what if you need that Constant’s value?

The great thing about constants in classes in this specific example is that you can access them like static variables outside of the class. For example:

1
2
3
4
$var = magicVarGettingFunction();
if ($var == MYCLASS::FLAG_ON) {
  print 'it is on';
}

This entry was posted in PHP and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>