Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:16197 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98064 invoked by uid 1010); 10 May 2005 13:54:12 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 72848 invoked from network); 10 May 2005 13:46:29 -0000 Received: from unknown (HELO suserver.com) (127.0.0.1) by localhost with SMTP; 10 May 2005 13:46:29 -0000 X-Host-Fingerprint: 200.32.3.79 200-32-3-79.prima.net.ar Linux 2.4/2.6 Received: from ([200.32.3.79:56913] helo=mail.bpservers.com.ar) by pb1.pair.com (ecelerity 1.2 r(5656M)) with SMTP id 38/0D-35155-23BB0824 for ; Tue, 10 May 2005 09:46:27 -0400 Received: (qmail 20520 invoked by uid 0); 10 May 2005 13:46:22 -0000 Received: from unknown (HELO ?192.168.0.240?) (200.42.80.195) by 200-32-3-79.prima.net.ar with SMTP; 10 May 2005 13:46:22 -0000 Reply-To: lpedretti@suserver.com Organization: Axon Sistemas To: Rory Browne Date: Tue, 10 May 2005 10:46:19 -0300 User-Agent: KMail/1.7.2 References: <200505091130.56300.lpedretti@suserver.com> <427FA7A5.6050803@iamjochem.com> In-Reply-To: Cc: internals@lists.php.net MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-ID: <200505101046.19099.lpedretti@suserver.com> Subject: Re: [PHP] Re: [PHP-DEV] make new return a reference to existent object From: lpedretti@suserver.com (Leonardo Pedretti) It would not be such a problem to write something that does the same task=20 through a static or global function, but unfortunately it is impossible, du= e=20 to the following: you can build a cache in an array, then in a __new()=20 function you could check if the requested object is already loaded and retu= rn=20 a reference. However, if this is the case, objects that enter the cache nev= er=20 leave it because they are still referenced in the array when all other=20 references have gone, so they never destruct, except when the array is=20 destroyed at program's end. So, the 'cache' would grow to include all possible objects, that's not good. Now, the question is not if there should be an 'existing' keyword for=20 replacing singletons... singletons are ok, but there may be some=20 circumstances when one would like to return some already existing instance = of=20 some class (between other existing instances, determined by the $id member= =20 for example) and get the destructor executed when all references to that=20 instance are gone. However, it is not possible to do it because there is no= t=20 any way to detect the removal of a reference to an object instance, except= =20 implementing specific (and really ugly) methods for assigning and deassigni= ng=20 the variables, such as __new(&$var) and __release(&$var) functions that do= =20 the task of incrementing and decrementing a reference count for the object= =20 and remove it from the cache when the refcount reaches 0. The main=20 disadvantage of this is that any request for an object of the mentioned cla= ss=20 should be matched with a __release() function much like in a C++ program=20 using new and delete. Is there any way of checking the existing instances of a class and returnin= g=20 the same instance in a function without implementing that cache system, so= =20 when all references are gone it naturally gets destroyed by the normal and= =20 natural php code flux? Leo On Monday 09 May 2005 20:28, Rory Browne wrote: > I think Leonardo might have a point here. Built-in support for > Singletons could be a nice feature, without the use of static > functions, would be nice. I wouldn't use the 'new' keyword however. I > think something along the lines of existing, or ref > > $ref =3D existing TEST; // which would return an existing instance of > TEST, if one exists, and a new one if it doesn't. ( pretty much like > the singleton code, except no need for singleton code) > > I don't think it's anything important, and there are more pressing > matters, to be worked on, I'm sure, but if someone created the code, > and it was secure / bug-free / compliant-with-coding-standards / etc, > I can't see why it couldn't(or shouldn't) be included. When all's said > and done though, it's still just syntactic sugar. It all depends on > how sweet the devs consider it. > > On 5/9/05, Jochem Maas wrote: > > Leonardo Pedretti wrote: > > > 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? > > > > not unless you hack the php engine (in which case > > your code will only work on your custom php build), > > I'll bet money that none of the php/zend devs feel anything for > > introducing this kind of 'magic' into engine. > > > > IMHO a factory would be a clean method of handling this behaviour and > > has the benefit that no programmer looking at your code will > > misinterpret occurances of the 'new' keyword. e.g.: > > > > class Test > > { > > /* ... */ > > function __construct() {} > > function get() {} > > } > > > > // and do: > > > > $var =3D Test::get( /* pass ctor args */ ); > > > > // instead of: > > > > $var =3D new Test( /* pass ctor args */ ); > > > > ... in this example its only 1 extra char to type when > > using the factory method and you could turn that around by > > doing something evil(tm) like: > > > > $var =3D Test::_( /* pass ctor args */ ); // '_' is a function name!!! = (ala > > wordpress-CS) > > > > maybe reply to generals describing what you are (trying to) do[ing], > > I for one am always interested in other people's realworld php5/oo > > problems/ideas/etc :-) > > > > rgds, > > Jochem > > > > ps - I redirected this reply to generals because I didn't really think > > that it was an internals question (and those guys are busy enough :-) > > > > > Thanx > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php =2D-=20 Leonardo Pedretti Axon Sistemas L=EDder de Equipo Proyecto Basalto