On 25/02/2016 22:11, Dan Ackroyd wrote:
It's been possible to do that in PDO for years, and people have been using it, and liking it, even if they weren't aware that 'under the hood' the PDO extension was doing the equivalent of calling 'new' as a callable.
Actually, PDO_FETCH_CLASS does something rather magic: it creates an object and actually injects individual property values into it. The constructor is not being used as a callback at all, and by default is called (with no parameters) *after* the properties have been initialised (a rather odd sequence, but there you go).
But why is it restricted to PDO? Why can't I do the equivalent with other DB libraries of my choice? Why can't I use it with userland libraries that retrieve data from Redis, Mongo or any other datasource? It has shown to be a useful pattern inside PDO. We should be empowering userland developers to have the same tools available to them as extensions do, wherever possible.
What exactly is stopping you doing it right now? Note that PDO has code written specifically for this case, which takes a class name and uses it in a particular way; if all it was doing was calling the constructor of that class, it would be trivial to implement in userland code: "new $classname($args)" works just fine, AFAIK.
The only part that is not possible right now - in PDO or anywhere else in the language - is to pass the constructor directly to a context that's expecting a callable. You have demonstrated some use cases where that would be convenient, but all of them can be implemented a different way reasonably easily.
Why should every PHP developer have to jump through that hoop, why can't we (as the RFC proposes) make constructors be callable, instead of having to create another method just to call something that should have been callable in the first place?
You talk of "jumping through hoops" and "should have been ... in the first place" as though it is self-evident that calling a constructor statically should imply object creation. A constructor is not a static method that somehow creates a new instance; rather, the "new" keyword is what creates the instance, and the constructor is an instance method called *after* creation, but before returning the new object to the caller.
It's important to remember that constructors and the "new" keyword are not concepts that PHP invented. I would like to join Adam in asking if you can find examples in other languages where constructors can be used as though they were static methods in this way. While there's nothing wrong with PHP being the only language to do something, there may be sound theory behind how other languages work.
Regards, -- Rowan Collins [IMSoP] -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php