Re: RFC: Custom Factories (SPL)
| From: | Ionut G. Stan | Date: | Thu, 19 Nov 2009 15:02:40 +0000 |
| Subject: | Re: RFC: Custom Factories (SPL) | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46104@lists.php.net to get a copy of this message | ||
It will work, just in a very complicated way. Emailer would need to have access to some other global variables in order to resolve its dependencies.
My point is that they are globals.
On 11/19/2009 4:57 PM, Mathieu Suen wrote:
Ionut G. Stan a écrit :-- Ionut G. Stan I'm under construction | http://blog.igstan.ro/This smells like metaclasses to me, just that in your RFC they are supposed to work on a global level, not on a per class level. Python, for example, has a magic method calledI have already made this proposition but it obviously don't work if Emailer need dependency.__new__, which controls the creation of the object whenever a new class is instantiated. It actually allows you to return a different object than the expected one. I somehow like the proposal, but it may create havoc in some code bases. I wouldn't really want to seenew FooBar, when it actually means aDamnBar. There are also other things to take into consideration. You may want to override this instantiation mechanism forFooBarjust in a single class, not in all of them, which complicates the problem. You'd have to received the scope where the instantiation is done, but even then, there may be no way to really know which *object*, not class, wants to instantiateFooBar. I agree IoC is necessary, but I'm not really sure this is the way to go. ... [some thinking] At this point, in PHP, classes are global constant symbols. Allowing us to override the instantiation mechanism would make them global variable symbols. The below example is already possible in PHP and I believe it already does what you're asking for (in the current RFC): class Emailer {} $Emailer= 'Emailer'; class FooBar { public function sendEmail() { global $Emailer; $emailer = new $Emailer; } }