|
Posted by Mathieu Maes on July 4, 2008, 4:50 am
Please log in for more thread options >
>
>
> > .oO(Gordon)
>
> > >I want to provide a set of static functions in a superclass that work
> > >with class constants defined in a decendant of that class.
> > >Unfortunately I've run into a snag with this idea. =A0Example:
>
> > >class SuperClass
> > >{
> > > =A0 =A0const CNST =A0 =A0 =A0=3D 'Super class';
>
> > > =A0 =A0public static function getCnst ()
> > > =A0 =A0{
> > > =A0 =A0 =A0 =A0 =A0 =A0return self::CNST;
> > > =A0 =A0}
> > >}
>
> > >class SubClass extends SuperClass
> > >{
> > > =A0 =A0const CNST =A0 =A0 =A0=3D 'Sub class';
> > >}
>
> > >echo (SubClass::getCnst ());
>
> > >I'd want the above code tou output "Sub Class", but instead it outputs
> > >"Super class".
>
> > >It would sem that static functions can't work with overridden
> > >constants in subclasses, or am I missing something?
>
> > Currently static references are resolved at compile time. What you need
> > here is called late static binding and scheduled for PHP 5.3.
>
> > Micha
>
> Thanks.
>
> Is there any kind of a workaround possible in older versions? =A0The
> only way I've found to get it to work as intended is to "inherit from
> the clipboard" and cut and paste the methods into all the classes.
> Maintainence nightmare.
Theoratically speaking, a constant that needs to change during
execution is not a constant.
Could you explain the context a bit more ? I don't really see what
you're trying to do in your "simple" example.
|