Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129329 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 334BA1A00BC for ; Thu, 20 Nov 2025 13:19:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1763644781; bh=7woZ/uRct5pNPJr4IPikbKZa8raZAQ7llpTg/NtbxuE=; h=From:Date:Subject:To:From; b=REvAu830Mx7tbO794luRgjhDhIvDGlTSjJH+zN1ueFGKLGtXuiHzLGOhwp9+nEXAr 7APDSNZ5DqcIfw8yiWr4IsuVpsPoYJw6JtTjuhqaHePaTGpdsiJpqPAJ7qAvZP99E2 cN5UY3ISwzPjYJGjmlspyFnj3yRlosj9e6xblWg2sL/F0P5+bM9J22SUeV3arKgKQ8 zAlkGTLJBzy9mthzxkFidyETT+4x53R/E2oH3hYlSgmkXD/irzvaknzXmpNS+E38yd GuiLbx8WaZcP7/XncNdFN6lyMeSw7C4oHQtbhaOF+niOZAR+/o7yE+1WdfqfVVaU/V 8PgRXdbMo1jiA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C751F18063C for ; Thu, 20 Nov 2025 13:19:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, T_SPF_TEMPERROR autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 20 Nov 2025 13:19:27 +0000 (UTC) Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-477b91680f8so7312715e9.0 for ; Thu, 20 Nov 2025 05:19:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763644761; x=1764249561; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=G341CqFTBEerrG09JLfgyUK+4WJdBXRvis3JLDRs8XM=; b=Vf71UPqKlXGAOEfkJvd0NuTNX8RycGfHoqNDqO7bjv0IgA5fEiwwwm6cqAH+0v9p5q jpAAgJHuSf8eK3rAgUiCq4nra3inlhB5J1t7xaoz2xTQUcyFJKfWVnomBFuzPs0ZbRws qBG9aWek73SNzJGSAZ6n/B/RS2imhSXbQTQF7+CrkVY2QG+Qmk0wzBv78Vw3WfAosrXb vEgXG7KE+Q7F0P+uiFcVAkAz0Zvr3RbPElvySDh0IVg8GGuQjazV4AYWJupqfnFqBaDs jUzcELhQ5BQ8j92Eg70orpbQSWKbePj+9LOd1QriOGF9Z3Eg/Z2rGzKLavaoupRUbEJe u2Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763644761; x=1764249561; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G341CqFTBEerrG09JLfgyUK+4WJdBXRvis3JLDRs8XM=; b=NPeiDC3my3chQdacc68AHG2Xmat6fI0NpUJ/6eb8lLjTXKvv4g8SxoZ/vxZTXSACx2 yR0xCg9XBHiaUY2rFy98VNfss7Q+tnoJKiCzScjlWqSuY2HyB6q3aELTYyuYpfzD7ZoO yFc8QzdY4wDo0MZBmJfS9lxziCPJxnLXjnYq+Og5fxy7UahhER7CIEYT+9M5r2axYRCJ y9dTTlXnD1iw3kDIS/v75g0JGtYrrEWvknF4hGsa6Jt+C+IYkV21Jux86d05hA1840/T uu0CdbwEyrYK4mShQ16oC3zYzuVI4qkgxF8rdiZtLoRhOtYfEvPf53ojcj6CqGSwZDmX xo7w== X-Gm-Message-State: AOJu0Yx9+qaTsjWLAKemuOHPWZiDXw4ym4wh+hXGJtpSX9LrQ99YnO64 +hT7koG4eUExUNy5b5GG73kovwimcADpqhPWlKKaUkmzF7XZCQrXAZi8UrXftYXdS8E2lwPeP9i 3Zb/sXsquwXV8CdRHe0axgfbO3cBZouW+zRo0Q2lFwyM= X-Gm-Gg: ASbGncv8uSgRml9bMEHtBigY1DynCseuFuhFX0xp/gCcs4W5gkBB1z1gwVhJS0jxBW+ yrU8+n4ouW7iOTIVxyEl6kqW78WQFQqmxyX3U0RkE0+9xfowKjj8YdQ1HCGJSe/p39dr3vbDLL3 lJT5TIwYW7zrMktO3wYJELmaS93lwJXBhCA/lS6NOXSQMwfkEkeYt7pVM+LPzkv2V8R/kvTJEV6 /uo8E0934mVdzEpd/9Y7IlWj8HPm6Jm1ErH5A1lN0SGP4S9XOumQbnSBtqmboPAEL+aANjUyLDX 7n4dzw== X-Google-Smtp-Source: AGHT+IEoU5Mfnw4sKUgdQFwUnoXaawCrhVkp5Y33GCEwkIOJ1NeZPqNQVMvfSlgHmYzzantbI75xJT6vBVz9EekxXug= X-Received: by 2002:a05:600c:1994:b0:477:9bfc:dcb6 with SMTP id 5b1f17b1804b1-477b895a8cemr31865525e9.14.1763644760649; Thu, 20 Nov 2025 05:19:20 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Thu, 20 Nov 2025 16:19:09 +0300 X-Gm-Features: AWmQ_bnFpMwq1XiCEynRJc2D1gkVXepLubuFxzaY1HLqZ6LFcPL5HK6_OvocVZY Message-ID: Subject: [PHP-DEV] First-class constructor callables To: php internals Content-Type: multipart/alternative; boundary="00000000000077f1b50644068b3b" From: udaltsov.valentin@gmail.com (Valentin Udaltsov) --00000000000077f1b50644068b3b Content-Type: text/plain; charset="UTF-8" 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 , 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 --00000000000077f1b50644068b3b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello internals,

I'd like to explore the p= ossibility of adding a first-class callable syntax for object constructors:=

```
$factory =3D new Foo(...);
```

The idea is that th= is expression would produce a Closure which, when invoked, calls the constr= uctor with the provided arguments:

```
$factory =3D new Foo(...);=
$object =3D $factory($arg1, $arg2);
```

This would conceptual= ly mirror the existing first-class callable notation for functions and meth= ods: trim(...), $object->method(...), Foo::method(...).

Today, th= e equivalent form requires boilerplate such as:

```
$factory =3D = static fn($arg): Foo =3D> new Foo($arg);
```

or defining a sta= tic factory:

```
class Foo
{
=C2=A0 =C2=A0 public static fu= nction new($arg): self
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 re= turn new self($arg);
=C2=A0 =C2=A0 }
}

$factory =3D Foo::new(.= ..);
```

However, the latter is not possible for 3rd-party classe= s.

Question for Larry and Arnaud:

In PFA v2, you n= ote that constructor references pose significant technical challenges.
C= ould you elaborate on what those challenges are and whether they are fundam= ental, or potentially addressable with a more limited or explicit syntax su= ch 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 =3D array_map(new BcMath\Number(...), $numericStrings);
`= ``

lazy objects do not help, and a constructor-as-callable form rema= ins valuable.

Best regards,
Valentin
--00000000000077f1b50644068b3b--