Override parent class constants

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux