Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:29161 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98167 invoked by uid 1010); 3 May 2007 16:58:49 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 98152 invoked from network); 3 May 2007 16:58:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 May 2007 16:58:48 -0000 Authentication-Results: pb1.pair.com smtp.mail=colder@php.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=colder@php.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain php.net from 213.239.212.54 cause and error) X-PHP-List-Original-Sender: colder@php.net X-Host-Fingerprint: 213.239.212.54 dns1.omne-serveurs.net Linux 2.4/2.6 Received: from [213.239.212.54] ([213.239.212.54:33387] helo=dns1.omne-serveurs.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9D/B1-27633-6C41A364 for ; Thu, 03 May 2007 12:58:47 -0400 Received: (qmail 15831 invoked from network); 3 May 2007 16:58:42 -0000 Received: from 84.74.75.107 by dns1 (envelope-from , uid 1004) with qmail-scanner-1.23 (clamdscan: 0.82. spamassassin: 3.0.1. perlscan: 1.32. Clear:RC:1(84.74.75.107):. Processed in 0.066989 secs); 03 May 2007 16:58:42 -0000 Received: from 84-74-75-107.dclient.hispeed.ch (HELO ?10.0.0.1?) (84.74.75.107) by dns1.omne-serveurs.net with (DHE-RSA-AES256-SHA encrypted) SMTP; 3 May 2007 16:58:42 -0000 Message-ID: <463A155D.1080702@php.net> Date: Thu, 03 May 2007 19:01:17 +0200 User-Agent: Thunderbird 1.5.0.7 (X11/20060909) MIME-Version: 1.0 To: Stanislav Malyshev CC: internals@lists.php.net References: <4631EE73.4050409@php.net> <4639F8D6.8020802@zend.com> In-Reply-To: <4639F8D6.8020802@zend.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [patch] Dynamic access of static members/methods, and constants From: colder@php.net (Etienne Kneuss) Hi, Stanislav Malyshev wrote: > > Could you describe a couple of use cases where you would use such > things (i.e. in real world scripts/libraries)? Sure, let's take an example: You have a library that uses a factory pattern, but you'd like to cache those already generated objects in a static variable in the class. Now, this feature is required in many classes and you'd not like to duplicate too much code, so a solution would be: class MySuperClass { private static $_objectsLoaded = array(); public static function factory($pkId, $classname = __CLASS__) { if(isset($_objectsLoaded[$classname][$pkId])) { return $_objectsLoaded[$classname][$pkId]; } $o = new $classname(); //load stuff from the database $table = $classname::$_table; $pkField = $classname::$_pkField; // build query using $table, and $pkField, load stuff into $o, etc.. if(!is_array(self::$_objectsLoaded[$classname])) { self::$_objectsLoaded[$classname] = array(); } self::$_objectsLoaded[$classname][$pkId] = $o; return $o; } public static function create($classname = __CLASS__) { $o = new $classname(); // ... return $o; } } class MyClass extends MySuperClass { protected static $_pkField = 'id'; protected static $_table = 'theTable'; public static factory($pkId, $classname = __CLASS__) { return parent::factory($pkId,$classname); } public static create($classname = __CLASS__) { return parent::create($classname); } // ... } There may be some typos in the code above that makes its syntax incorrect, but the idea is here. Now, as you can see, because late static binding is not yet implemented and probably never will be because of the overhead it might introduce, you've to pass the classname as a parameter and use it to request things from the child's class, at this point, being able to do that using that $classname:: shortcut is quite elegant, instead of having to use overkill call_user_func calls as all those _get*() methods would have to be redefined in every the child classes. I wouldn't mind having to use multiple call_user_func calls for that purpose if I knew it was too hard to implement that $classname:: thing, but it's so easy that I really believe it's a feature worth having. And PHP is also about multiple ways of doing something, right ? :) -- Etienne Kneuss http://www.colder.ch colder@php.net Men never do evil so completely and cheerfully as when they do it from a religious conviction. -- Pascal