> >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?
It'll be slow no matter what you do, because you need to dynamically reference the constant value. You could also use the switch construct you already posted, or use the solution suggested by Ard; all of these are slow, particularly switch when used with a large number of string 'case's. You might actually find that the eval works out faster. If you're really thinking of writing high performance code in PHP, you shouldn't be writing code that has 100's of class definitions ;-) > 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 :) The engine only stores child->parent relationships, not child<->parent relationships. Think about it for a moment... can you do this kind of thing in compiled languages? Do you know why you can't? The reason is that the compiler has no way of knowing what classes are going to extend it at the time it compiles the base class. This is why I suggested that trying to dynamically access a constant (eg: compile time!) of a child class just seems wrong. --Wez. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php