Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the value of the constant is variable, then it is not a constant, but a variable. </p> <p>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.</p> <p>Some resources:</p> <ul> <li><a href="http://www.patternsforphp.org/index.php?title=Registry" rel="nofollow noreferrer">http://www.patternsforphp.org/index.php?title=Registry</a></li> <li><a href="http://www.brandonsavage.net/the-registry-pattern-reexamined/" rel="nofollow noreferrer">http://www.brandonsavage.net/the-registry-pattern-reexamined/</a></li> <li><a href="http://blog.fedecarg.com/2007/10/26/registry-pattern-or-dependency-injection-container/" rel="nofollow noreferrer">http://blog.fedecarg.com/2007/10/26/registry-pattern-or-dependency-injection-container/</a></li> </ul> <hr> <p><strong>EDIT</strong> If you are truly desperate and <em>have to have</em> the constant redefined, you can use </p> <ul> <li><a href="http://de.php.net/manual/en/function.runkit-constant-redefine.php" rel="nofollow noreferrer"><code>runkit_constant_redefine</code></a> — Redefine an already defined constant</li> </ul> <p>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)</p> <pre><code>if ( file_exists('xyc') ) { runkit_constant_redefine('Constants::USERNAME', 'xyz'); } </code></pre> <hr> <p><strong>EDIT</strong> Some more options (all of which not exactly pretty either):</p> <pre><code>class Abc { const FOO = 1; const BAR = 2; } class Xyz extends Abc { const FOO = 2; } class_alias(file_exists('abc') ? 'Abc' : 'Xyz', 'Constants'); </code></pre> <p>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 <code>Constants::USERNAME</code>, 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.</p> <p>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 <em>xyz</em> and then <code>include</code> either or depending on the file check.</p> <p>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 <code>eval</code> the string, effectively including the class this way.</p> <p><strong>But I have to say it again: I strongly suggest refactoring your code over using these.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload