[PHP-DEV] [VOTE] Support Closures in constant expressions

Hi

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

RFC: PHP: rfc:closures_in_const_expr
Implementation: Allow defining Closures in const-expr by TimWolla · Pull Request #16458 · php/php-src · GitHub
Discussion: RFC: Support Closures in constant expressions - Externals

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

Best regards
Tim Düsterhus

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: PHP: rfc:closures_in_const_expr
Implementation: Allow defining Closures in const-expr by TimWolla · Pull Request #16458 · php/php-src · GitHub
Discussion: RFC: Support Closures in constant expressions - Externals

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

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: And what about this one ? Curious for your reply. Smile, Juliette

···

On 27-11-2024 15:03, Tim Düsterhus wrote:

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

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 ?

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 ?

Hi

On 11/27/24 22:50, Juliette Reinders Folmer wrote:

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 (Online PHP editor | output for 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.