Re: Re: [RFC] Callable constructors
| From: | Rowan Collins | Date: | Thu, 25 Feb 2016 20:28:08 +0000 |
| Subject: | Re: Re: [RFC] Callable constructors | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91421@lists.php.net to get a copy of this message | ||
On 25/02/2016 17:40, Andrea Faulds wrote:
Dan Ackroyd wrote:Snipped for brevity, but I agree with your sentiment here. Making __construct more magic seems like an imperfect solution to this. I'm not sure making a new magic method (new, __new, etc) is much better though, since it would serve such a limited purpose. It has come up before in various contexts that PHP lacks a true callable type, in the sense that you never "construct" a callable, you just *use* something as a callable. I think callable constructors would fit better as part of an API that solved this. For instance, there could be a (magic) Callable class with various named constructors: - Callable::forFunction(string $function_name) - Callable::forMethod(object $instance, string $method_name) - Callable::forStaticMethod(string $class_name, string $method_name) - Callable::fromPaamayimNekudotayim(string $class_colon_colon_method) // OK, maybe not ;) This isn't perfect, because the parameters would still look like strings to static analysis, rather than being proper compiled-in references, but it's nicer than repeatedly analysing an array or string to evaluate is_callable() or type-checks. Then we could add to that a special case for constructors: - Callable::forConstructor(string $class_name) This would be roughly equivalent to (and polyfillable with): return function (...$args) use ( $class_name ) { return new $class_name(...$args); }; Regards, -- Rowan Collins [IMSoP]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/callableconstructorsWhile I like the concept, I dislike the execution.