So constants are constant, no kidding, eh?

But why to hell can’t I assign variable content to it? Wouldn’t make sense, you say? Constants ARE constant?

ASSIGN – NOT REassign, buddy!

You won’t find a solution in this text. There are workarounds all over the net – that’s not the issue here – I simply had the urge to nag, complain an moan about that fact .

Figure this (just making up this example for demonstration, but it’s as good as any):
You have a class SpitOutFQUserImageURL to get you your users profile picture’s fully qualified URL.
(yeah, you read right – FQ is not a new abbreviation for Fucking :))

Right on – you’ve got your basic config file with all the well defined constants – you want to be as hardware and software agnostic as you can be.

First you have the protocol defined in PROTOCOL: http://
You got the constant SITEBASE : Let it be fileyourthoughts.com.
You got the subdomain defined in SUBDOMAIN: www
Then there is MEDIAPATH: mediastore
Finally there’s IMAGEPATH being: userimages

Now that class is simple enough, does what it has to do bla bla bla and then a method builds the FQ URL:
public function spitItOut() {
$urlBase = PROTOCOL . SUBDOMAIN . '.' . SITEBASE . '/' . MEDIAPATH . '/' . IMAGEPATH . '/' . $this->getYourFuckingImage($userId);
return $urlBase;
}

Yeah yeah, we could just leave it like that and it will work.

But I don’t like it! Why can’t I – for readability if not anything else – define a class constant USERIMAGEPATH like that:

class SpitOutFQUserImageURL {
const USERIMAGEPATH = PROTOCOL . SUBDOMAIN . '.' . SITEBASE . '/' . MEDIAPATH . '/' . IMAGEPATH . '/';
}

and have my method read like:

public function spitItOut() {
$userImg = self::USERIMAGEPATH . $this->getYourFuckingImage($userId);
return $userImg;
}

Halt! Don’t copy that stuff above – it won’t work!
I wish it would!

Why?

If you’re working on larger projects you work with packages, that, if done right, can be re-used in other projects (or part of the same project) w/o doing to much adjustments.
Now imagine this class here is part of a package that does all the image handling you need and you want to use it somewhere else – different project, different environment, DIFFERENT configuration.

  • What are you going to do? Right, browse the code, find every fucking reference to the config file constants and adjust – good luck, have fun.

Versus

  • You just go to the top of your class and do the adjustments there.

I could have used a variable and assign the constants there, you say?. Yeah, I could have in the above example – although as the code grows so do these things tend to grow with it. Get’s you one hell of a constructor with lots of ‘work-around’ junk in it.

But what if the method was static???
class SpitOutFQUserImageURL {
const USERIMAGEPATH = PROTOCOL . SUBDOMAIN . '.' . SITEBASE . '/' . MEDIAPATH . '/' . IMAGEPATH . '/';

public static function spitItOut($userId) {
$userImg = self::USERIMAGEPATH . self::getYourFuckingImage($userId);
return $userImg;
}
}

Where would I define that varibale now?
(Again, that above example WON’T work! Again I wish it would)

As I said – you can work around everything but my conclusion is:
PHP class constant assignment sucks!

PS: YES, I DO indent my code, but this godforsaken editor keeps outdenting it…

PPS: This was written looking at PHP 5.2.3