rfc:calls_in_constant_expressions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:calls_in_constant_expressions [2020/02/09 20:10] – Document that method calls are compile errors tandre | rfc:calls_in_constant_expressions [2020/02/09 20:46] – tandre | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Allow function calls in constant expressions ====== | ====== PHP RFC: Allow function calls in constant expressions ====== | ||
- | * Version: 0.2 | + | * Version: 0.3 |
* Date: 2020-02-21 | * Date: 2020-02-21 | ||
* Author: Tyson Andre < | * Author: Tyson Andre < | ||
- | * Status: | + | * Status: |
* First Published at: https:// | * First Published at: https:// | ||
Line 18: | Line 18: | ||
This RFC proposes allowing global function calls by name (in any namespace). | This RFC proposes allowing global function calls by name (in any namespace). | ||
- | Method | + | Method |
Function calls will be allowed in the following types of constant expressions: | Function calls will be allowed in the following types of constant expressions: | ||
Line 27: | Line 27: | ||
* Defaults of static variables (evaluated and cached the first time the expression succeeds) | * Defaults of static variables (evaluated and cached the first time the expression succeeds) | ||
- | There are two secondary voting | + | A secondary |
- | These voting | + | That voting |
Allowing any function to be called in a constant expression will allow more flexibility for users, | Allowing any function to be called in a constant expression will allow more flexibility for users, | ||
but limiting function names to a whitelist will make side effects of uses of constants easier to reason about when developers read php code. | but limiting function names to a whitelist will make side effects of uses of constants easier to reason about when developers read php code. | ||
Line 35: | Line 35: | ||
==== Proposal for Whitelisting ==== | ==== Proposal for Whitelisting ==== | ||
- | + | If the secondary vote passes, | |
- | The first option is to allow any function (user-defined or internal) | + | It will be left to coding practice guidelines to assert that constants are only |
- | leave it to coding practice guidelines to assert that constants are only | + | |
used in safe ways. | used in safe ways. | ||
- | The second option is to only allow a whitelist functions that are actually deterministic and without side effects, and don't depend on ini | + | If the secondary vote fails, then only functions in a whitelist of functions will be callable in constant expressions. |
+ | Attempting to use other functions in constant expressions will be a fatal compile error. | ||
+ | This whitelist | ||
settings or locale. The functions must be unambiguously resolved. | settings or locale. The functions must be unambiguously resolved. | ||
This has the same implementation as the first option, with additional compile-time restrictions. | This has the same implementation as the first option, with additional compile-time restrictions. | ||
Line 65: | Line 66: | ||
will all throw an '' | will all throw an '' | ||
- | Argument unpacking is allowed. (e.g. '' | + | Argument unpacking is allowed. (e.g. '' |
Function calls must be by name: | Function calls must be by name: | ||
Line 83: | Line 84: | ||
the first time they get used. | the first time they get used. | ||
- | Parameter defaults | + | Parameter defaults |
Opcache is free to cache the result if the parameter values are known and the function is definitely deterministic. | Opcache is free to cache the result if the parameter values are known and the function is definitely deterministic. | ||
The behavior of constant expressions that don't contain function calls won't be modified by this RFC. | The behavior of constant expressions that don't contain function calls won't be modified by this RFC. | ||
- | |||
- | ==== Behaviors in Constant Expressions ==== | ||
- | |||
- | For class constants, static property defaults, and static variable defaults: | ||
- | If the evaluation of a constant expression throws, the result will not be cached. | ||
- | The expression will be evaluated repeatedly every time the constant expression gets used, | ||
- | and will get cached permanently the first time it doesn' | ||
==== Handling functions not in the whitelist ==== | ==== Handling functions not in the whitelist ==== | ||
Line 139: | Line 133: | ||
For this reason, '' | For this reason, '' | ||
- | The list of functions is below: | + | The list of functions |
abs | abs | ||
Line 266: | Line 260: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Primary vote: Allow calling global functions that are in the described whitelist (Yes/No, Requires 2/3 majority) | + | Primary vote: Allow calling global functions |
Secondary vote: Allow calling any global functions, without a whitelist limitation (Yes/No, Requires 2/3 majority for " | Secondary vote: Allow calling any global functions, without a whitelist limitation (Yes/No, Requires 2/3 majority for " |
rfc/calls_in_constant_expressions.txt · Last modified: 2020/03/04 14:01 by tandre