Click here to get back home

Overriding class constants

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Overriding class constants Gordon 07-03-2008
Posted by Gordon on July 3, 2008, 9:18 am
Please log in for more thread options
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. Example:

class SuperClass
{
        const CNST        = 'Super class';

        public static function getCnst ()
        {
                return self::CNST;
        }
}

class SubClass extends SuperClass
{
        const CNST        = '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?

Posted by Jerry Stuckle on July 3, 2008, 11:25 am
Please log in for more thread options
Gordon wrote:
> 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. Example:
>
> class SuperClass
> {
>         const CNST        = 'Super class';
>
>         public static function getCnst ()
>         {
>                 return self::CNST;
>         }
> }
>
> class SubClass extends SuperClass
> {
>         const CNST        = '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?
>

Nope, unfortunately, this is true.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Posted by Michael Fesser on July 3, 2008, 11:29 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. Example:
>
>class SuperClass
>{
>        const CNST        = 'Super class';
>
>        public static function getCnst ()
>        {
>                return self::CNST;
>        }
>}
>
>class SubClass extends SuperClass
>{
>        const CNST        = '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

Posted by Gordon on July 4, 2008, 4:14 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. Example:
>
> >class SuperClass
> >{
> > const CNST = 'Super class';
>
> > public static function getCnst ()
> > {
> > return self::CNST;
> > }
> >}
>
> >class SubClass extends SuperClass
> >{
> > const CNST = '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? The
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.

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.

Similar ThreadsPosted
[PHP4] accessing constants outside class body December 21, 2005, 12:24 pm
Why do constants have quotes around when defined and class quotes have no quotes?? December 11, 2007, 9:00 am
Problem with overriding Functions (oop) January 19, 2008, 6:32 pm
Overriding/Replacing a method in the PHP installation. August 22, 2004, 3:04 pm
Php constants in php.ini November 27, 2005, 9:53 pm
Use of OLE constants in PHP July 29, 2007, 12:55 pm
PHP, OOP, and Constants December 18, 2007, 10:37 am
Getting Apache Constants with PHP June 5, 2007, 9:07 pm
Re: List Constants July 23, 2007, 4:21 pm
Re: List Constants July 23, 2007, 5:12 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap