rfc:fcc_in_const_expr
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:fcc_in_const_expr [2025/01/07 13:19] – created timwolla | rfc: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), | * Author: Tim Düsterhus (tim@tideways-gmbh.com), | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | The elevator pitch for the RFC. The first paragraph of this section will be slightly larger to give it emphasis; please write a good introduction. | + | |
+ | The [[rfc: | ||
+ | |||
+ | 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:// | + | This RFC proposes that it shall be legal to use the FCC syntax |
- | for inclusion | + | |
- | Remember that the RFC contents should be easily reusable | + | * If a referenced class does not yet exist, |
+ | * 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. | ||
- | If applicable, you may wish to use the language specification as a reference. | + | ==== 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 (< | ||
+ | * Only the standard < | ||
+ | * Referencing methods that implicitly exist due to the existence of a < | ||
+ | |||
+ | ==== Scoping ==== | ||
+ | |||
+ | As with other constant-expressions, | ||
+ | |||
+ | ==== Examples ==== | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | |||
+ | # | ||
+ | class Attr { | ||
+ | public function __construct(public Closure $value) {} | ||
+ | } | ||
+ | |||
+ | # | ||
+ | # | ||
+ | class C { | ||
+ | private static function myMethod(string $foo) { | ||
+ | return " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | foreach ((new ReflectionClass(C:: | ||
+ | $closure = $reflectionAttribute-> | ||
+ | var_dump($closure(' | ||
+ | } | ||
+ | |||
+ | // Prints: | ||
+ | // | ||
+ | // string(3) " | ||
+ | // string(3) " | ||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | What breaks, and what is the justification for it? | + | |
+ | None. Using first class callables within constant expressions previously resulted in a compile-time error. Closures can appear within constant expressions since the [[rfc: | ||
+ | |||
+ | Nevertheless, as with every RFC that changes | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 38: | 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.1736255974.txt.gz · Last modified: 2025/01/07 13:19 by timwolla