Is it possible to redefine PHP constants?
Is it possible to redefine class constants (in PHP)?
e.g.
class B {
const C_ThisIsAConstant = 1;
}
class A extends B {
self::C_开发者_如何学PythonThisIsAConstant = 2;
}
No, of course not. Then they wouldn't be "constants."
You can't redefine "constants" because they are contant.
If you're trying to change a constant defined in an included file, then you can defining the constant before the include:
define ("PROCESS_NAME", "MIKE");
/* ... code ... */
include ("/path/to/included_file.php"); // also defines "PROCESS_NAME"
/* ... code ... */
PROCESS_NAME will be "MIKE".
First: No, it is not possible to redefine class constants. Its impossible in every language, because otherwise a constant wouldnt be constant.
But what you are doing is possible, because you dont redefine a class constant, instead you define a one unique constant for every class.
<?php
class B {
const C_ThisIsAConstant = 1;
}
class A extends B {
const C_ThisIsAConstant = 2;
}
var_dump(A::C_ThisIsAConstant);
It outputs int(2)
.
http://codepad.org/GQR9HI5M
I have published my framework YAPAF on github.com. Let's call it alpha-state. Anyhow, YAPAF is able to change the value of a class constant (see https://github.com/SchulteMarkus/YAPAF/blob/master/tests/ManipulateConstantTest.php).
精彩评论