# [VOTE] Support Closures in constant expressions

- Thread: https://externals.io/message/125950
- Original archive: https://news-web.php.net/php.internals/125950
- Started by tim@bastelstu.be on 2024-11-13T13:40:32+00:00
- Messages: 5
- RFC: Support Closures in constant expressions (Implemented (PHP 8.5)) — https://externals.io/rfc/closures_in_const_expr.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #125950 — tim@bastelstu.be — 2024-11-13T13:40:32+00:00

URL: https://externals.io/message/125950#125950

Hi

we just started the vote the the "Support Closures in constant 
expressions" RFC. Please find the following resources for your 
reference:

RFC: https://wiki.php.net/rfc/closures_in_const_expr
Implementation: https://github.com/php/php-src/pull/16458
Discussion: https://externals.io/message/125872

The vote will end in two weeks, on November, 27th 2024 at 14:00 UTC.

Best regards
Tim Düsterhus

---

## #126069 — tim@bastelstu.be — 2024-11-27T14:03:27+00:00

URL: https://externals.io/message/125950#126069 · In reply to: #125950

Hi

Am 2024-11-13 14:40, schrieb Tim Düsterhus:
> we just started the vote the the "Support Closures in constant 
> expressions" RFC. Please find the following resources for your 
> reference:
> 
> RFC: https://wiki.php.net/rfc/closures_in_const_expr
> Implementation: https://github.com/php/php-src/pull/16458
> Discussion: https://externals.io/message/125872
> 
> The vote will end in two weeks, on November, 27th 2024 at 14:00 UTC.

Voting just closed. The RFC was accepted unanimously with 19 (Yes) to 0 
(No) votes. Thank you for your participation.

Best regards
Tim Düsterhus

---

## #126074 — Juliette Reinders Folmer — 2024-11-27T21:50:12+00:00

URL: https://externals.io/message/125950#126074 · In reply to: #126069

> Hi
>
> Am 2024-11-13 14:40, schrieb Tim Düsterhus:
> > […]
>
> Voting just closed. The RFC was accepted unanimously with 19 (Yes) to 
> 0 (No) votes. Thank you for your participation.
>
> Best regards
> Tim Düsterhus
>

Congrats on the passed RFC! Sounds like a useful feature.

One thing I'm wondering about - and of which I saw no mention in the RFC 
nor in the preceding discussion - knowing that function names are 
case-insensitive, how is ambiguity handled when _calling_ a callback 
stored in a (class) constant ?

Consider the following code sample:

const STRTOUPPER = static function (string $text): string {
      return $text !== '' ? \strtoupper($text) : '';
};

STRTOUPPER('test'); // What will this do ? Call the PHP native function 
or the closure ?


And what about this one ?

class Foo {
     public const STRTOUPPER = static function (string $text): string {
          return $text !== '' ? \strtoupper($text) : '';
     };

     public function strtoupper(string $text) {
         return \ucfirst($text);
     }

     public function test() {
         self::STRTOUPPER('test'); // What will this do ?
     }
}

Foo::STRTOUPPER('test'); // What will this do ?


Curious for your reply.

Smile,
Juliette

---

## #126075 — tim@bastelstu.be — 2024-11-27T21:58:39+00:00

URL: https://externals.io/message/125950#126075 · In reply to: #126074

Hi


> One thing I'm wondering about - and of which I saw no mention in the RFC
> nor in the preceding discussion - knowing that function names are
> case-insensitive, how is ambiguity handled when _calling_ a callback
> stored in a (class) constant ?

Nothing changed about the grammar or the way a PHP program is parsed 
into Opcodes. As mentioned in the RFC it does not include any backwards 
incompatible changes, except for making some illegal PHP programs legal.

In fact you are already able to store Closures in dynamic constants 
defined with the `define()` function (https://3v4l.org/u7ZJ1).

In any case that means that all your examples are interpreted as 
function calls. You need parentheses around the constant name to 
"dereference" the value stored in the constant. This is consistent with 
$obj->foo() always being a method call to the foo() method, not a call 
to a Closure stored in a property called $foo.

Best regards
Tim Düsterhus

---

## #126076 — Juliette Reinders Folmer — 2024-11-27T22:05:18+00:00

URL: https://externals.io/message/125950#126076 · In reply to: #126075

> Hi
>
> 
> > […]
>
> Nothing changed about the grammar or the way a PHP program is parsed 
> into Opcodes. As mentioned in the RFC it does not include any 
> backwards incompatible changes, except for making some illegal PHP 
> programs legal.
>
> In fact you are already able to store Closures in dynamic constants 
> defined with the `define()` function (https://3v4l.org/u7ZJ1).
>
> In any case that means that all your examples are interpreted as 
> function calls. You need parentheses around the constant name to 
> "dereference" the value stored in the constant. This is consistent 
> with $obj->foo() always being a method call to the foo() method, not a 
> call to a Closure stored in a property called $foo.
>
> Best regards
> Tim Düsterhus
>

Thanks for the clarification. Might be good to mention this in the 
documentation eventually.
