Re: [RFC] Callable constructors

From: Date: Thu, 25 Feb 2016 18:27:23 +0000
Subject: Re: [RFC] Callable constructors
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-91416@lists.php.net to get a copy of this message
On Thu, Feb 25, 2016 at 9:44 AM, Dan Ackroyd <danack@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.

« previous php.internals (#91416) next »