Re: Override parent class constants

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

 



I hate to disappoint you, but there's no real alternative. Same annoyance
with get_class() and __CLASS__ always giving you the "class in which the
call is defined" instead of the class which is actually being called when
dealing with static methods.

What you could do is define a protected static method in every subclass,
something like getConstant(), which always gives you the constant from the
correct class. I can't come up with a better solution than that. And I've
been trying to find one since the first version of php5 ;-)

On 22/08/07, James Ausmus <james.ausmus@xxxxxxxxx> wrote:
>
> 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
>
>


-- 
Interpotential.com
Phone: +31615397471

[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