On 20 November 2025 13:19:09 GMT, Valentin Udaltsov 
<[email protected]> wrote:
>
>I'd like to explore the possibility of adding a first-class callable syntax
>for object constructors:
>
>```
>$factory = new Foo(...);
>```
>
>The idea is that this expression would produce a Closure which, when
>invoked, calls the constructor with the provided arguments:
>
>```
>$factory = new Foo(...);
>$object = $factory($arg1, $arg2);
>```


Something worth clarifying here is that what you want is not just to *call the 
constructor*, but to *create the object*. In some languages, that's the same 
thing - there's a base object constructor which has to be called - but in PHP, 
the constructor itself is just a callback hook, called *after* the object 
exists.

And that's where the complexity comes in: "new Foo" isn't a function call, it's 
an interpreter operation.

Here is Nikita's explanation of the issue in the First Class Callable RFC 
<https://wiki.php.net/rfc/first_class_callable_syntax>

[Begin Quote]

The new Foo() syntax is not considered a call, and as such also not supported 
by the new Foo(...) syntax. It should be noted that there is also no way to 
express object creation with traditional callable syntax, and it is thus also 
not supported by Closure::fromCallable().

The general expectation is that new Foo(...) would be creating a new instance 
of Foo on each call, rather than creating a single instance of Foo and then 
repeatedly calling the constructor. To support this, it would effectively be 
necessary to generate a trampoline function of the form

fn(...$args) => new Foo(...$args)

and acquire a callable to that trampoline instead. While certainly possible, 
this takes a step backwards from the straightforward semantics of the foo(...) 
syntax for ordinary calls.

[End Quote]


Rowan Tommins
[IMSoP]

Reply via email to