On Thu, Feb 25, 2016 at 9:44 AM, Dan Ackroyd <dan...@basereality.com> wrote:
> Hello internals, > > I've written a small RFC to make it possible to call constructors of > objects as a callable, rather than only being able to call them > through the 'new' language construct. > > https://wiki.php.net/rfc/callableconstructors > > After gathering informal feedback for the RFC, one person said that > they felt it needed more of an emphasis on why it should be > implemented. I wanted to keep the actual RFC short, in the hope that > more people would read it in it's entirety, and also wanted to leave > library specific code out of the RFC. So, as an addendum to the RFC > text: > > I use the Auryn* DIC library. What I've wanted to do, and should be > able to do in my opinion, is this: > > $injector->delegate('FooInterface', 'FooImplementation::__construct'); > Which tells the injector "Whenever something requests an object that > implements FooInterface as a dependency, just execute the > FooImplementation constructor to make that object". > How is this any better, or what benefit would it provide over the current implementation, which takes a class name and then calls new on it when the Interface is asked for? Moreover, if you want to pass in a callable that creates the class, why not just create a method on that class called create (or whatever) much the same way that singletons often work.