CM.
CM.

Reputation: 680

PHP constants declaration based on condition

I am using one separate file for all constants of my PHP application.

class constants
{
    const USERNAME = 'abc';
    /*
      ⋮
    */
}

For lets say USERNAME constant, value can be either xyz or abc based on file exists check. If xyz file exists USERNAME value would be xyz. How can I do this check in my constants class?

Upvotes: 7

Views: 8703

Answers (4)

Gordon
Gordon

Reputation: 316969

If the value of the constant is variable, then it is not a constant, but a variable.

Since you are (correctly) trying to keep your application stuff capsuled and away from the global scope, you might be interested in the Registry Pattern. A Registry is basically a glorified array that stores whatever you throw into it and is globally accessible throughout your application.

Some resources:


EDIT If you are truly desperate and have to have the constant redefined, you can use

Runkit might not be available if you are on shared hosting and I consider needing it a code smell, but here is how you'd basically do it (in your bootstrap)

if ( file_exists('xyc') ) {
    runkit_constant_redefine('Constants::USERNAME', 'xyz');
}

EDIT Some more options (all of which not exactly pretty either):

class Abc { const FOO = 1; const BAR = 2; }
class Xyz extends Abc { const FOO = 2; }
class_alias(file_exists('abc') ? 'Abc' : 'Xyz', 'Constants');

For this you would rename your current constants class to Abc and add a second class Xyz to extend it and overwrite the USERNAME constant (FOO in the example). This would obviously break your code, because you used to do Constants::USERNAME, so you have to create an alias for the former class name. Which class Constants will point to, is decided with the conditional check. This requires PHP5.3.

A pre-5.3 solution would be to simply save the Constants class file under two different names, e.g. abc_constants.php and xyz_constants.php, modify the latter accordingly to hold USERNAME xyz and then include either or depending on the file check.

Or replace the value of USERNAME with a placeholder and instead of including the class you load it into a variable as a string. Then you replace the placeholder according to the filecheck result and eval the string, effectively including the class this way.

But I have to say it again: I strongly suggest refactoring your code over using these.

Upvotes: 6

Azeem.Butt
Azeem.Butt

Reputation: 5861

https://www.php.net/manual/en/function.define.php

if ( $file_exists )
{
    define('MYCONSTANT', 'YEAH');
}
else
{
    define('MYCONSTANT', 'NOPE');
}

Upvotes: 0

elias
elias

Reputation: 1469

It's not possible do have conditional constant definitions inside classes.

Upvotes: 0

MartyIX
MartyIX

Reputation: 28648

Isn't this: PHP Readonly Properties? approach easier?

It seems to me you try to misuse constants.

Upvotes: 0

Related Questions