Possible Duplicate:
Can you assign values to constants with equal sign after using defined in php?
I开发者_运维技巧'm not sure if it's just me, but how do you override an existing constant something like this:
define('HELLO', 'goodbye');
define('HELLO', 'hello!');
echo HELLO; <-- I need it to output "hello!"
//unset(HELLO); <-- unset doesn't work
//define('HELLO', 'hello!');
You can override a constant if it extended from a class. So in your case you can't override constant as it consider came from a same class. ie (taken from php manual):
<?php
class Foo {
const a = 7;
const x = 99;
}
class Bar extends Foo {
const a = 42; /* overrides the `a = 7' in base class */
}
$b = new Bar();
$r = new ReflectionObject($b);
echo $r->getConstant('a'); # prints `42' from the Bar class
echo "\n";
echo $r->getConstant('x'); # prints `99' inherited from the Foo class
?>
If you turn on php error reporting ie:
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
you will see a notice like
Notice: Constant HELLO already defined in ....
Truth is, you can, but you should not. PHP being an interpreted language, there is nothing you "can't" do. The runkit extension allow you to modify PHP internals behavior, and provide the runkit_constant_redefine(simple signature) function.
If the page is reloading, you can have a dynamic value change the constant.
Like:
$random = something_that_gives_me_randomness();
define('HELLO', $random);
But if you are trying to change a constant in the same script, then linepogl is correct. Its called a constant for a reason.
精彩评论