Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:4774 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 47380 invoked by uid 1010); 10 Oct 2003 10:44:12 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 47293 invoked from network); 10 Oct 2003 10:44:10 -0000 Received: from unknown (HELO mx.thebrainroom.net) (65.200.24.98) by pb1.pair.com with SMTP; 10 Oct 2003 10:44:10 -0000 Received: by mx.thebrainroom.net (Postfix, from userid 517) id 3DABF1488087; Fri, 10 Oct 2003 03:42:37 -0700 (PDT) Received: from zaneeb.thebrainroom.net (zaneeb.thebrainroom.net [82.133.1.138]) by mx.thebrainroom.net (Postfix) with ESMTP id D38D11488087; Fri, 10 Oct 2003 03:42:34 -0700 (PDT) Received: from titan (titan.thebrainroom.net [82.133.1.139]) by zaneeb.thebrainroom.net (8.11.6/8.11.6) with SMTP id h9AAi7K14978; Fri, 10 Oct 2003 11:44:07 +0100 Message-ID: <001601c38f1b$6e5132d0$8b018552@titan> To: "Dan Cox" , References: <3F865B5F.9090606@wep.net> Date: Fri, 10 Oct 2003 11:44:07 +0100 Organization: The Brain Room Ltd. MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.3790.0 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0 X-Spam-Status: No, hits=-0.8 required=5.0 tests=AWL,QUOTED_EMAIL_TEXT,REFERENCES version=2.55 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.55 (1.174.2.19-2003-05-19-exp) X-TBR-Filter: Virus scanned and defanged Subject: Re: [PHP-DEV] Accessing child constants from base class From: wez@thebrainroom.com ("Wez Furlong") 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" To: 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