First-class constructor callables

From: Date: Thu, 20 Nov 2025 13:19:09 +0000
Subject: First-class constructor callables
Groups: php.internals 
Request: Send a blank email to internals+get-129329@lists.php.net to get a copy of this message
Hello internals, 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); ``` This would conceptually mirror the existing first-class callable notation for functions and methods: trim(...), $object->method(...), Foo::method(...). Today, the equivalent form requires boilerplate such as: ``` $factory = static fn($arg): Foo => new Foo($arg); ``` or defining a static factory: ``` class Foo { public static function new($arg): self { return new self($arg); } } $factory = Foo::new(...); ``` However, the latter is not possible for 3rd-party classes. Question for Larry and Arnaud: In PFA v2 <https://wiki.php.net/rfc/partial_function_application_v2#constructors>, you note that constructor references pose significant technical challenges. Could you elaborate on what those challenges are and whether they are fundamental, or potentially addressable with a more limited or explicit syntax such as new Foo(...)? You also mention that "the use cases for partially applying a constructor are few, especially now that we have lazy objects (as of PHP 8.4)." I tend to disagree. For cases like: ``` $numbers = array_map(new BcMath\Number(...), $numericStrings); ``` lazy objects do not help, and a constructor-as-callable form remains valuable. Best regards, Valentin

« previous php.internals (#129329) next »