Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:13731 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75924 invoked by uid 1010); 5 Nov 2004 16:01:20 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 75818 invoked from network); 5 Nov 2004 16:01:19 -0000 Received: from unknown (HELO amber.vis-av.com) (66.92.75.243) by pb1.pair.com with SMTP; 5 Nov 2004 16:01:19 -0000 Received: (qmail 10694 invoked from network); 5 Nov 2004 15:55:52 -0000 Received: from unknown (HELO random.?none?) (192.168.1.9) by amber.internal with SMTP; 5 Nov 2004 15:55:52 -0000 Received: (nullmailer pid 18290 invoked by uid 0); Fri, 05 Nov 2004 15:55:51 -0000 To: internals Reply-To: Derrell.Lipman@UnwiredUniverse.com Date: Fri, 05 Nov 2004 10:55:51 -0500 Message-ID: User-Agent: Gnus/5.110003 (No Gnus v0.3) XEmacs/21.4 (Common Lisp, linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: new overloading feature? From: Derrell.Lipman@UnwiredUniverse.com 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? 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 */ ?>