Override parent class constants
| From: | James Ausmus | Date: | Wed, 22 Aug 2007 20:04:33 +0000 |
| Subject: | Override parent class constants | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-261127@lists.php.net to get a copy of this message | ||
Hello - I'm trying to find a (sane) way to, in an extended class,
override the parent class's constants, something like the following
(which doesn't actually work):
class baseClass
{
const myBaseVar = "base value!";
protected $myVar;
function __construct()
{
$this->myVar = self::myBaseVar;
echo $this->myVar;
}
}
class extClass
{
const myBaseVar = "overriden value!";
}
And, when instanciated, should do the following:
$bc = new baseClass();
Output: base value!
$ec = new extClass();
Output: overridden value!
Any way to do that?
There's a hacky way to do it, which would be to change the
__contruct() function to the following:
function __construct()
{
$this->myVar = eval("echo " . get_class($this) . "::myBaseVar;");
echo $this->myVar;
}
but that is really, really ugly - I don't want to have to change all
instances of myVar re-initialization in my baseClass methods into eval
statements...
Any thoughts?
-James