Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46099 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28778 invoked from network); 19 Nov 2009 11:06:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Nov 2009 11:06:25 -0000 Authentication-Results: pb1.pair.com header.from=mathieu.suen@easyflirt.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=mathieu.suen@easyflirt.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain easyflirt.com designates 91.199.255.56 as permitted sender) X-PHP-List-Original-Sender: mathieu.suen@easyflirt.com X-Host-Fingerprint: 91.199.255.56 python-06.easyrencontre.com Linux 2.6 Received: from [91.199.255.56] ([91.199.255.56:33141] helo=mail.easyflirt.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 42/84-01102-CA6250B4 for ; Thu, 19 Nov 2009 06:06:24 -0500 Received: from [127.0.0.1] (office.easyrencontre.com [78.155.152.6]) by mail.easyflirt.com (Postfix) with ESMTPSA id 43C306375AF for ; Thu, 19 Nov 2009 12:06:18 +0100 (CET) Message-ID: <4B0526AB.50104@easyflirt.com> Date: Thu, 19 Nov 2009 12:06:19 +0100 User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 CC: internals@lists.php.net References: <20091119022332.GFJH21638.aamtaout02-winn.ispmail.ntl.com@p2> <09.30.10882.96FB40B4@pb1.pair.com> In-Reply-To: <09.30.10882.96FB40B4@pb1.pair.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] RFC: Custom Factories (SPL) From: mathieu.suen@easyflirt.com (Mathieu Suen) Alban a écrit : > Le Thu, 19 Nov 2009 02:24:01 +0000, Jared Williams a écrit : > >>> -----Original Message----- >>> From: Robert Lemke [mailto:robert@typo3.org] Sent: 18 November 2009 >>> 16:07 >>> To: internals@lists.php.net >>> Subject: [PHP-DEV] RFC: Custom Factories (SPL) >>> >>> Hi folks, >>> >>> after discussing the idea with various PHP developers I now felt safe >>> enough that it's not a completely stupid idea to post an RFC for it. >>> The idea is to add support the registration of custom factories which >>> are responsible for instantiating certain classes. >>> >>> Here is the first draft of my RFC: >>> http://wiki.php.net/rfc/customfactories >>> >>> I suggest that we first discuss the implications and usefulness of this >>> feature. In a second step I'd need to find some skilled internals >>> wizard who can implement it, because not being a C developer myself, >>> all I can offer is making suggestions and fine coffee. >>> >>> Looking forward to hearing your comments! Robert >>> >>> -- >>> Robert Lemke >>> Fluent Code Artisan >>> >>> >> Whilst I am a fan of IoC, I don't think its desirable to add language >> features just to make legacy code more flexible. Surely the route to >> take is to refactor the legacy code as and when the extra flexibility is >> needed. >> >> Also seems like a possible whole heap of wtf?! When a seemingly absolute >> statement $a = new A(); gets mangled behind the scenes. >> >> Jared > > I'm totaly agree with you Jared ! > I'm a fan of IoC too. > > A factory can provide an object by creating an instance of a class, the > choice of the class depends of some paramters. Those parameters can be > provided directly by the programmer but also by a final user. > PHP can not simply "choose" the class to instanciate by checking class's > interfaces. > > What Robert Lemke wants to implement is called a "Service" or a > "Container". > You register a service and call the service instead of calling a class > directly. > > Let me show it in an example of session registration : > > // you register the service > Services::register('session', 'sessionStoredInCookie'); > > // and call it > $session = Services::load('session'); > > Tomorrow, if you want change the session registration mechanism, for > storing them in a database for exemple, you have just to change the > registred service and whole application has change its session > registration method : > > // you register the service > Services::register('session', 'sessionStoredInDatabase'); > > This is very well treated by Fabien Potencier in this document : http:// > fabien.potencier.org/talk/20/decouple-your-code-for-reusability-php- > forum-2008?position=41 (some parts are in french) > > Implementing a service or container mechanism could be very simple or > very complicated. Fabien Potencier use a very complicated example in the > link above. > > Use a service or container mechanism (and its implementation) is a > developper choice. this could not be traced by php core. > > A service (or container) can create an instance of class or just return > an instance of class which is allready created. > > // you register the service > Services::register('databaseConnexion', new databaseConnexionFactory > ('mysql', array('server', 'database', 'user', 'password'))); > > // and call it > $db = Services::load('databaseConnexion'); > > How PHP should treat the singleton ? getInstance() is just a convention. > Create a service mechanism directly in php implies that php implements > singleton model too. > Some language have support for IoC. See newspeak: http://gbracha.blogspot.com/2009/07/ban-on-imports-continued.html -- Mathieu Suen