Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:4771 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 8538 invoked by uid 1010); 10 Oct 2003 07:08:27 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 8513 invoked from network); 10 Oct 2003 07:08:26 -0000 Received: from unknown (HELO spike.wep.net) (66.171.16.54) by pb1.pair.com with SMTP; 10 Oct 2003 07:08:26 -0000 Received: from wep.net (internal [192.168.1.1]) by spike.wep.net (8.12.8/8.12.8) with ESMTP id h9A7ANEb019811 for ; Fri, 10 Oct 2003 03:10:23 -0400 Message-ID: <3F865B5F.9090606@wep.net> Date: Fri, 10 Oct 2003 03:10:23 -0400 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20030929 X-Accept-Language: en-us, en MIME-Version: 1.0 To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Accessing child constants from base class From: dan@wep.net (Dan Cox) 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