This sounds like you're doing something wrong (no offense!).

You want to access a *constant* of a descendant class, when
your ancestor doesn't even know if it exists.
Well, that sounds more than a little odd (backwards even).

Why not just use a property with a known name, and set the
value of that property in you descendant class constructor?

Performance wise, its not going to make much difference,
because no matter what you are doing, to dynamically resolve
the value of a constant will involve hash lookups.

The other alternative, and this is the official POV of the
Zend guys IIRC, is that you can use eval() to look up
the value:

$node = $doc->createElement(eval(get_class($this) . "::ElementName"));

If you think about it, what exactly does child:: refer to anyway?
A child class of the current object? But which one?  What if
the child doesn't have the constant?  What if there are interfaces
involved?

The engine doesn't know about descendant classes either (the
inheritance tree works in the other direction), and it shouldn't
have to second-guess what your code is doing - its much clearer to
explicitly write code like that eval above.

Hope that helps!

--Wez.



----- Original Message ----- 
From: "Dan Cox" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, October 10, 2003 8:10 AM
Subject: [PHP-DEV] Accessing child constants from base class


> PHP5 10/10/2003 CVS.
>
> Currently, I don't see an easy way of accessing the constants of a child
> class from a base class. Consider the following basic example:
>
> abstract class myBaseClass {
>   function getXML() {
>     $doc = new domDocument();
>     $node = $doc->createElement(child::ElementName);
>     $doc->appendChild($node);
>     return $doc->saveXML($node);
>   }
> }
>
> class myChildClass extends myBaseClass {
>   const ElementName = 'foo';
>   // ..lots of methods, etc here..
> }
>
> $foo = new myChildClass();
> print($foo->getXML());
>
>
> Of course, this does not work as there is no 'child::' accessor. The
> alternatives
> to making this work are:
>
> Use a protected variable in the child (and, of course, don't forget to
> also say
> 'protected $ElementName;' in the base class) and use $this->ElementName
> for accessing the child's ElementName from the parent. This is probably
good
> enough, but much less than ideal.
>
> Use this beautifully low maintenance, high performance piece of code in
the
> base class (not):
>
> $className = get_class($this);
> switch($className) {
>   case 'mychildclass':
>     $childElementName = myChildClass::ElementName;
>   break;
>   case 'myotherchildclass':
>     $childElementName = myOtherChildClass::ElementName;
>   break;
> // etc.....
> }
> .... ick!
>
> You may be able to shortcut the need for the switch statement with some
> variable variable and/or eval() trickery, but let's not even go there..
>
> So are there any plans on implementing child:: ? Is there a way of
accessing
> child constants without having to explicitly known the current and/or
> child class
> name?
>
> Any input appreciated-
> Dan Cox

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to