Hi Wez-

Wez Furlong wrote:

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?



Yes, this is how I'm doing things now. It just seems like I
shouldn't be forced to always have to use
parent::__construct(_MY_CONSTANT_)

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"));



Yes. Normally, at least with most other programming languages,
using eval() is a major performance hit (as much as 10x slower),
so it shouldn't be used unless there is absolutely no other way.
Maybe this isn't the case with PHP?

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?



A deriving class of the current *instance* .. so childInstance::
then :) Perhaps you could use interfaces to enforce children
having the needed constants.

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.



I suppose. It just seems odd that a class can talk to it(self::)
and it's parent:: but not its child:: even though the child::
instance started the conversation in the first place :)

Dan Cox

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



Reply via email to