Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:33387 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19472 invoked by uid 1010); 23 Nov 2007 09:57:19 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 19457 invoked from network); 23 Nov 2007 09:57:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Nov 2007 09:57:19 -0000 Authentication-Results: pb1.pair.com header.from=marco.kaiser@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=marco.kaiser@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 64.233.182.187 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: marco.kaiser@gmail.com X-Host-Fingerprint: 64.233.182.187 nf-out-0910.google.com Received: from [64.233.182.187] ([64.233.182.187:17893] helo=nf-out-0910.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 52/A2-02878-CF3A6474 for ; Fri, 23 Nov 2007 04:57:18 -0500 Received: by nf-out-0910.google.com with SMTP id e27so2524131nfd for ; Fri, 23 Nov 2007 01:57:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:from:to:cc:references:in-reply-to:subject:date:mime-version:content-type:content-transfer-encoding:x-mailer:thread-index:content-language:message-id; bh=DKaAP+wJNfyaDehCjqiMVoLLZZ3rnVpsuxAurqQ/geM=; b=FMHpJSupGVyd08OaWEQe3wxiL3GQioYQq07p9uD6l+I0PCCXQPrKPBigLVp2CzcDds8MnczrvPEMqeyfp5XC8WJUJj76HqVvguYHLAbcggfYh/Ajy+UBkM8qprkuso9ZvR/47Mt4IkF146/bj0LLwM7c/VXwy9661No/kfxi1hc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:from:to:cc:references:in-reply-to:subject:date:mime-version:content-type:content-transfer-encoding:x-mailer:thread-index:content-language:message-id; b=IzYIFGg8b3/7rcKQmqHOaaYch/Dm1BtreL7tjlsjQhxvZEEWOvu6Yd8ej8SP+o9DxiwYttkoNjag36Q6ewWFUoMwWkZDaxPeOArC9a0dfqeEExMPd7oNBlaVy4Gb5zEU8UwR5Z9MEf/On6EC2v2Kqp6atRzsjQFAyDT3exD9Rgg= Received: by 10.86.78.4 with SMTP id a4mr8087092fgb.1195811833430; Fri, 23 Nov 2007 01:57:13 -0800 (PST) Received: from think ( [217.7.237.75]) by mx.google.com with ESMTPS id y18sm2713707fkd.2007.11.23.01.57.11 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 23 Nov 2007 01:57:12 -0800 (PST) To: Cc: "'PHP Developers Mailing List'" , 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> In-Reply-To: <10845a340711230121j1e06040bw15953d02330b622@mail.gmail.com> Date: Fri, 23 Nov 2007 10:53:11 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AcgtskhFZEZUXiMER1C1PdvEYf3IywAAdz8g Content-Language: de Message-ID: <4746a3f8.12975e0a.018f.3392@mx.google.com> Subject: RE: [PHP-DEV] late static binding php6 From: marco.kaiser@gmail.com ("Marco Kaiser") Hi Richard, the problem is this: NEW INSTANCE object(foo)#1 (0) { } NEW INSTANCE object(bar)#2 (0) { } object(foo)#1 (0) { } object(bar)#2 (0) { } This means i just called once the constructor for the singleton abstract = for every main class (foo, bar) But if you remove the static member $_instance from the foo and bar = class you get this result: NEW INSTANCE object(foo)#1 (0) { } NEW INSTANCE object(bar)#2 (0) { } NEW INSTANCE object(foo)#1 (0) { } NEW INSTANCE object(bar)#2 (0) { } You see that the constructor is called every time if you use more than 1 = singleton class during script execution. The problem is that the property isnt really inherited by the foo and = bar class. My idea or a possible solution would be to allow the definition of = correct inheritance of the $_instance property or to show me a way to set a static property dynamicly in the caller class. Maybe Andi or Markus can explain this a bit more why this isnt possible. = :) -- Marco > -----Original Message----- > From: Richard Quadling [mailto:rquadling@googlemail.com] > Sent: Friday, November 23, 2007 10:22 AM > To: Marco Kaiser > Cc: PHP Developers Mailing List > Subject: Re: [PHP-DEV] late static binding php6 >=20 > 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 =3D get_called_class(); > > > if (!static::$_instance instanceof $caller) { > > > static::$_instance =3D new $caller; > > > } > > > > > > return static::$_instance; > > > } > > > } > > > > > > class foo extends singleton { > > > static protected $_instance =3D null; > > > } > > > > > > class bar extends singleton { > > > static protected $_instance =3D 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 =3D null; > > > > > > > > static public function getInstance() > > > > { > > > > $caller =3D get_called_class(); > > > > if (!static::$_instance instanceof $caller) { > > > > static::$_instance =3D 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 =3D null; > > static public function getInstance() > > { > > $caller =3D get_called_class(); > > if (!static::$_instance instanceof $caller) { > > static::$_instance =3D 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. >=20 > Also, you can make the getInstance() method final so it cannot be > overridden in sub-classes... >=20 > abstract class singleton > { > static protected $_instance =3D null; > static final public function getInstance() > { > $caller =3D get_called_class(); > if (!static::$_instance instanceof $caller) { > static::$_instance =3D new $caller; > } >=20 > return static::$_instance; > } > } >=20 > class foo extends singleton { > public function getInstance() { return 'foobar'; } > } >=20 > class bar extends singleton { > } >=20 > var_dump(foo::getInstance()); > var_dump(bar::getInstance()); > var_dump(foo::getInstance()); > var_dump(bar::getInstance()); > ?> >=20 > returns ... >=20 > Fatal error: Cannot override final method singleton::getInstance() in > C:\sing.php on line 18 >=20 > So, a very useful addition to the language. >=20 > -- > ----- > Richard Quadling > Zend Certified Engineer : > http://zend.com/zce.php?c=3DZEND002498&r=3D213474731 > "Standing on the shoulders of some very clever giants!"