Re: Accessing child constants from base class
| From: | Dan Cox | Date: | Fri, 10 Oct 2003 08:53:20 +0000 |
| Subject: | Re: Accessing child constants from base class | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-4773@lists.php.net to get a copy of this message | ||
Marcus-
Marcus Börger wrote:
Hello Dan,the base class knows the name of (unlikely). You could figure things out dynamically, as I provided some example code for, but this would become unwieldy with 10s to 100s of derived classes.Currently, I don't see an easy way of accessing the constants of a child class from a base class.Use the child's class name: php -r 'class t{static function f(){echo tt::c;}}class tt extends t{const c="Hello\n";} t::f();' Yes, this is possible, but it assumes you always only ever have one child class, which
But i guess you want something dynamically as $class::const, right? right.
the next example uses 'self' which doesn't work because self is bound at compile time: php -r 'class t{function t(){echo self::c;}}class tt extends t{const c= "Hello\n";} $o=new tt;' So you'd need something like '$this::c' which is impossible right now. Funny thing is the following which returns NULL what makes absolute no sense to me. It somehow looks like either the correct constant is used but it is uninitialized or there is an error missing. php -r 'class t{function t(){var_dump($this->c);}}class tt extends t{const c= "Hello\n";} $o=new tt;' I believe this is correct behaviour. When instantiating an object the methods, properties,etc. of the child class and any and all parents are in essence aggregated together into a new object (the instance). It would make sense (at least to me) that 'class constants' should NOT be aggregated together on instantiation. They should be constant only for their particular class definition and all code provided by that class. So you could have a const foo = 'bar' in both a parent and child class, and they would be 2 unique constants only accessable by code inside their class definition (how it works right now). I take this position because, from a programmer standpoint, when i type 'const foo', I would like to believe I'm defining a *constant* name/value pair that could not be changed at runtime. If constants are aggregated together between all parent/child class definitions on instantiation, then there could be the potential for conflicts and overriding of constant values. But I digress.. I just would like an easy way to get to child constants. Having to use something like myClass::constant from *inside* an object instance *of* myClass seems kind of... strange. I imagine that conversation looking something like this: Scene: A family instance of a Parent and Child are standing side by side. They talk amongst themselves for a few moments, and then.. Parent speaks: "Mr. Zend Engine, I need to access a static class constant." Zend: "OK, Parent, a constant of whom?" Parent: "A constant of my Child. Do you know where I can find my Child?" Zend: "Errr.. well.. yeah.. your Child is right there!" *points finger* Parent: "Hey! There's my Child and he's got the constant! Thanks Zend, you know
*everything*!"
Zend: *scratches head and thinks* "Why didn't he just ask him himself instead of wasting
my time?"
Dan Cox