Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:33385 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10123 invoked by uid 1010); 23 Nov 2007 09:18:14 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 10108 invoked from network); 23 Nov 2007 09:18:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Nov 2007 09:18:14 -0000 Authentication-Results: pb1.pair.com smtp.mail=rquadling@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rquadling@googlemail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.146.183 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rquadling@googlemail.com X-Host-Fingerprint: 209.85.146.183 wa-out-1112.google.com Received: from [209.85.146.183] ([209.85.146.183:23330] helo=wa-out-1112.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8F/F0-02878-0DA96474 for ; Fri, 23 Nov 2007 04:18:14 -0500 Received: by wa-out-1112.google.com with SMTP id l24so3805654waf for ; Fri, 23 Nov 2007 01:18:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=4YdBxeCmrpCUDxBXDCgNHO8Y4SSUrWOZfwiJEbSs2QQ=; b=jGubZZS47Yfyfap1E7YvFJtZdEQxuB9rg2QdA9zELuH1iJDvjGjlyCXTA7tQ1XcChn9eb7XAWyFze3VnsrzTwSDwgEKoxTqKMFRZG9OduDTr3RZ2LTQD6Ig5DeA0tD1/ziu6MIqcSUzOgVW6B5MxsCiLLK45gk8+vih6CcZwQa8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=OYVamOY1N3nu2IO9g4IsAxce5JY0OxDBE+JbEGolrK580cZMsh4FCLytVM0tKPO7NI9+tvDihg8kzMnciMfgtpC9dXsUieg5uPWUPtwm05PyLLYfMJeTF5GGpVEvLzkOIjIhGqvASZLQjhxp6x/2/6z7RXV6sVSfW11jjoK+PxQ= Received: by 10.114.88.1 with SMTP id l1mr943799wab.1195809486261; Fri, 23 Nov 2007 01:18:06 -0800 (PST) Received: by 10.114.210.8 with HTTP; Fri, 23 Nov 2007 01:18:05 -0800 (PST) Message-ID: <10845a340711230118t6c19243cg812f22858e3a3c75@mail.gmail.com> Date: Fri, 23 Nov 2007 09:18:05 +0000 Reply-To: RQuadling@GoogleMail.com To: "Marco Kaiser" Cc: "PHP Developers Mailing List" In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <4740C654.3020302@digitalsandwich.com> <474130B3.9070303@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> Subject: Re: [PHP-DEV] late static binding php6 From: rquadling@googlemail.com ("Richard Quadling") 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 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. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!"