Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:33389 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41218 invoked by uid 1010); 23 Nov 2007 11:22:36 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 41203 invoked from network); 23 Nov 2007 11:22:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Nov 2007 11:22:36 -0000 Authentication-Results: pb1.pair.com header.from=helly@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=helly@php.net; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 85.214.94.56 as permitted sender) X-PHP-List-Original-Sender: helly@php.net X-Host-Fingerprint: 85.214.94.56 aixcept.net Linux 2.6 Received: from [85.214.94.56] ([85.214.94.56:59761] helo=h1149922.serverkompetenz.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A0/E5-02878-AF7B6474 for ; Fri, 23 Nov 2007 06:22:36 -0500 Received: from dhcp-172-28-204-176.zrh.corp.google.com (unknown [193.142.125.1]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by h1149922.serverkompetenz.net (Postfix) with ESMTP id 7C6B91B364D; Fri, 23 Nov 2007 12:22:31 +0100 (CET) Date: Fri, 23 Nov 2007 12:22:30 +0100 Reply-To: Marcus Boerger X-Priority: 3 (Normal) Message-ID: <388489975.20071123122230@marcus-boerger.de> To: "Richard Quadling" CC: "Marco Kaiser" , "PHP Developers Mailing List" In-Reply-To: <10845a340711230121j1e06040bw15953d02330b622@mail.gmail.com> References: <4740C654.3020302@digitalsandwich.com> <4741DC81.6000506@zend.com> <4741D574.2020800@digitalsandwich.com> <474247A2.5050301@zend.com> <4741D9D1.6030106@digitalsandwich.com> <47424B4F.2@zend.com> <4743CED7.5050402@avalon.aut.bme.hu> <10845a340711230118t6c19243cg812f22858e3a3c75@mail.gmail.com> <10845a340711230121j1e06040bw15953d02330b622@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] late static binding php6 From: helly@php.net (Marcus Boerger) Hello Richard, that kind of stuff is the reason we added LSB. There is only one tiny thing I am not to happy about. You do not at all have to declare static $instance. Simply because you never use it. And for that reason you guys have your code worng. Use the following snippet for verification: var_dump($u=foo::getInstance()); var_dump($v=bar::getInstance()); var_dump($w=foo::getInstance()); var_dump($x=bar::getInstance()); var_dump(array($u,$v,$w,$x)); When you want to have a factory of singletons that can create one single member of every derived class than you need to add an array to store those inside the abstract base class. That also allows to make that member private and thereby disallowing any derived class to interfere with the static member and get rid of them to overthrow the singleton limitation... You also have no need for the instanceof operation with the array. See here: Friday, November 23, 2007, 10:21:51 AM, you wrote: > On 23/11/2007, Richard Quadling wrote: >> On 22/11/2007, Marco Kaiser wrote: >> > Hi again, >> > >> > to explain the main idea a bit more, the code below work and moves the >> > main getInstance function from the class and its possible to abstract >> > this. >> > it would be cool to get the protected property also into the abstract >> > class. Any idea or maybe a solution in the near future? >> > >> > > > abstract class singleton >> > { >> > static public function getInstance() >> > { >> > $caller = get_called_class(); >> > if (!static::$_instance instanceof $caller) { >> > static::$_instance = new $caller; >> > } >> > >> > return static::$_instance; >> > } >> > } >> > >> > class foo extends singleton { >> > static protected $_instance = null; >> > } >> > >> > class bar extends singleton { >> > static protected $_instance = null; >> > } >> > >> > var_dump(foo::getInstance()); >> > var_dump(bar::getInstance()); >> > var_dump(foo::getInstance()); >> > var_dump(bar::getInstance()); >> > ?> >> > >> > On Nov 22, 2007 9:29 PM, Marco Kaiser wrote: >> > > Hi List, >> > > >> > > just to drop my note here, i asked (i think) 2 years ago for such a >> > > feature to automate my singleton pattern. Not with late static >> > > bindings this is possible. >> > > >> > > > > > class singleton >> > > { >> > > static protected $_instance = null; >> > > >> > > static public function getInstance() >> > > { >> > > $caller = get_called_class(); >> > > if (!static::$_instance instanceof $caller) { >> > > static::$_instance = new $caller; >> > > } >> > > >> > > return static::$_instance; >> > > } >> > > } >> > > >> > > class foo extends singleton >> > > { >> > > } >> > > >> > > var_dump(foo::getInstance()); >> > > var_dump(foo::getInstance()); >> > > ?> >> > > >> > > i think this will also drop much redundant code from some frameworks. >> > > So this is one of my examples that helps much. >> > > >> > > >> > > -- >> > > Marco Kaiser >> > > >> > >> > >> > >> > -- >> > Marco Kaiser >> >> > abstract class singleton >> { >> static protected $_instance = null; >> static public function getInstance() >> { >> $caller = get_called_class(); >> if (!static::$_instance instanceof $caller) { >> static::$_instance = new $caller; >> } >> >> return static::$_instance; >> } >> } >> >> class foo extends singleton { >> } >> >> class bar extends singleton { >> } >> >> var_dump(foo::getInstance()); >> var_dump(bar::getInstance()); >> var_dump(foo::getInstance()); >> var_dump(bar::getInstance()); >> ?> >> >> returns ... >> >> object(foo)#1 (0) { >> } >> object(bar)#2 (0) { >> } >> object(foo)#1 (0) { >> } >> object(bar)#2 (0) { >> } >> >> in PHP 5.3.0-dev (cli) (built: Nov 20 2007 08:19:12) >> >> I think this is great! Well done everyone. Unless I've completely >> missed the point. > Also, you can make the getInstance() method final so it cannot be > overridden in sub-classes... > abstract class singleton > { > static protected $_instance = null; > static final public function getInstance() > { > $caller = get_called_class(); > if (!static::$_instance instanceof $caller) { > static::$_instance = new $caller; > } > return static::$_instance; > } > } > class foo extends singleton { > public function getInstance() { return 'foobar'; } > } > class bar extends singleton { > } > var_dump(foo::getInstance()); > var_dump(bar::getInstance()); > var_dump(foo::getInstance()); > var_dump(bar::getInstance()); ?>> > returns ... > Fatal error: Cannot override final method singleton::getInstance() in > C:\sing.php on line 18 > So, a very useful addition to the language. > -- > ----- > Richard Quadling > Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 > "Standing on the shoulders of some very clever giants!" Best regards, Marcus