rfc:fcc_in_const_expr

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:fcc_in_const_expr [2025/01/07 13:21] timwollarfc:fcc_in_const_expr [2025/01/22 14:31] (current) – Set to under discussion edorian
Line 3: Line 3:
   * Date: 2025-01-07   * Date: 2025-01-07
   * Author: Tim Düsterhus (tim@tideways-gmbh.com), Volker Dusch (volker@tideways-gmbh.com)   * Author: Tim Düsterhus (tim@tideways-gmbh.com), Volker Dusch (volker@tideways-gmbh.com)
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/fcc_in_const_expr   * First Published at: http://wiki.php.net/rfc/fcc_in_const_expr
  
 ===== Introduction ===== ===== Introduction =====
-The elevator pitch for the RFC. The first paragraph of this section will be slightly larger to give it emphasis; please write good introduction.+ 
 +The [[rfc:closures_in_const_expr|Support Closures in constant expressions]] RFC added support for Closures in constant expressions. The RFC left supporting “[[rfc:first_class_callable_syntax|First Class Callables]]” (“FCC”), which can be considered syntax sugar for creating Closure that just forwards any calls to the callable in question. 
 + 
 +Given that FCCs effectively act as syntax sugar, the same use cases as with Closures themselves also apply to this RFC. It is intended to round off the “Closures in constant expressions” RFC.
  
 ===== Proposal ===== ===== Proposal =====
-All the features and examples of the proposal. 
  
-To [[http://news.php.net/php.internals/66051|paraphrase Zeev Suraski]], explain hows the proposal brings substantial value to be considered +This RFC proposes that it shall be legal to use the FCC syntax in constant expressions. The semantics match the existing semantics of FCCs and of constant expressions. Aside from the obvious, this means: 
-for inclusion in one of the world's most popular programming languages.+ 
 +  * If a referenced class does not yet exist, the autoloader is triggered, as with <php>new</php>-expressions in constant expressions. 
 +  * If a relative reference to a free-standing function is used in a namespaced context, the function will first be looked up within the namespace, falling back to the global namespace if it does not exist. 
 + 
 +==== Constraints ==== 
 + 
 +All the constraints imposed on FCCs outside of constant expressions naturally also apply to FCCs within constant expressions. Defining a FCC within a constant expression comes with the following additional constraints: 
 + 
 +  * Only references to free-standing functions and to static methods (<php>::</php>) are supported. 
 +  * Only the standard <php>function_name(...)</php> or <php>ClassName::methodName(...)</php> references are supported. The function name must not be an expression (constant or otherwise). <php>[ClassName::class, 'methodName'](...)</php><php>(Constant)(...)</php>, or similar are not supported. 
 +  * Referencing methods that implicitly exist due to the existence of a <php>__callStatic()</php> method on the referenced class are not supported. 
 + 
 +==== Scoping ==== 
 + 
 +As with other constant-expressions, FCC defined in constant expressions follow the expected scoping rules of the context they are placed inThis means that FCC in property default values //may// reference <php>private</php> methods of the class where they are defined, similarly to how a FCC defined in the constructor and stored in a property may access those <php>private</php> methods. Likewise are FCC in attribute parameters allowed to access <php>private</php> methods of the surrounding class. 
 + 
 +==== Examples ==== 
 + 
 +<PHP> 
 +<?php 
 + 
 +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] 
 +class Attr { 
 +    public function __construct(public Closure $value) {} 
 +
 + 
 +#[Attr(self::myMethod(...))] 
 +#[Attr(strrev(...))] 
 +class C { 
 +    private static function myMethod(string $foo) { 
 +        return "XXX"; 
 +    } 
 +}
  
-Remember that the RFC contents should be easily reusable in the PHP Documentation.+foreach ((new ReflectionClass(C::class))->getAttributes() as $reflectionAttribute) { 
 +    $closure = $reflectionAttribute->newInstance()->value; 
 +    var_dump($closure('abc')); 
 +}
  
-If applicable, you may wish to use the language specification as a reference.+// Prints: 
 +// 
 +// string(3) "XXX" 
 +// string(3) "cba" 
 +</PHP>
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 42: Line 83:
 ==== To Opcache ==== ==== To Opcache ====
  
-tbd+The current implementation uses a special AST structure to cache the resolved function for consistent behavior of the global function fallback. To correctly cache this AST structure, Opcache changes are required. The PR passes all tests both with and without JIT enabled.
  
 ==== New Constants ==== ==== New Constants ====
rfc/fcc_in_const_expr.1736256076.txt.gz · Last modified: 2025/01/07 13:21 by timwolla