Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:16181 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32774 invoked by uid 1010); 9 May 2005 16:47:20 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 5803 invoked from network); 9 May 2005 16:24:21 -0000 Received: from unknown (HELO unwireduniverse.com) (127.0.0.1) by localhost with SMTP; 9 May 2005 16:24:21 -0000 X-Host-Fingerprint: 66.92.75.243 dsl092-075-243.bos1.dsl.speakeasy.net Linux 2.4/2.6 Received: from ([66.92.75.243:3731] helo=amber.vis-av.com) by pb1.pair.com (ecelerity 1.2 r(5656M)) with SMTP id 6C/BD-35155-4BE8F724 for ; Mon, 09 May 2005 12:24:20 -0400 Received: (qmail 10836 invoked from network); 9 May 2005 15:17:39 -0000 Received: from unknown (HELO random.internal) (192.168.1.9) by amber.internal with SMTP; 9 May 2005 15:17:39 -0000 Received: (nullmailer pid 15081 invoked by uid 0); Mon, 09 May 2005 16:17:32 -0000 To: internals References: Reply-To: Derrell.Lipman@UnwiredUniverse.com Date: Mon, 09 May 2005 12:17:32 -0400 In-Reply-To: (Derrell Lipman's message of "Fri, 05 Nov 2004 10:55:51 -0500") Message-ID: <4qdcz84j.fsf@random.internal> User-Agent: Gnus/5.110003 (No Gnus v0.3) XEmacs/21.4.17 (linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: Re: new overloading feature? From: Derrell.Lipman@UnwiredUniverse.com Derrell.Lipman@UnwiredUniverse.com writes: Leonardo Pedretti writes: > I would like (for code cleanliness purposes) to make 'new' return a > reference to an already created object under certain circumstances without > using a factory, is it possible? A number of months ago, I proposed a similar feature, implemented as overloading a __new() function. At the time, it seemed I was the only one who wanted it, but your request is for a feature identical to my request. Here's my previous message. (The whole thread can be found under the subject "new overloading feature?" from 5 Nov 2004). I'd love for this to be revisited now that there's someone else with a similar desire. My previous proposal: > I came across an interesting desire today. I'd like to create a new class > instance if an only if a "key" value does not already exist. This key value > could be looked up in a database, in an array, etc. > > The following contrived example shows use of a proposed __new() overload > function which would be called BEFORE the constructor, and could chose to > return a newly constructed object (by calling __construct()) or to return an > already existing object. > > One could certainly call a function which searched for the key value and only > instantiated a new object if the existing one was not found, but this seems > cleaner. > > Thoughts? > > > class X > { > static $allX = array(); > var $val; > > function __construct($val) > { > $this->val = $val; > X::$allX[] =& $this; > } > > function __new($val) > { > foreach (X::$allX as $x) > { > if ($x->val == $val) > { > return $x; > } > } > > return __construct($val); > } > } > > $try1 = new X(23); /* would return $allX[0] reference */ > $try2 = new X(42); /* woudl return $allX[1] reference */ > $try3 = new X(23); /* would return $allX[0] reference */ > ?>