Ionut G. Stan a écrit :
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 called `__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 see `new FooBar`, when it actually means a
`DamnBar`.
There are also other things to take into consideration. You may want to
override this instantiation mechanism for `FooBar` just 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
instantiate `FooBar`.
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;
}
}
I have already made this proposition but it obviously don't work if
Emailer need dependency.
On 11/18/2009 6:06 PM, Robert Lemke wrote:
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
-- Mathieu Suen
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php