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

Reply via email to