rfc:calls_in_constant_expressions

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
Next revisionBoth sides next revision
rfc:calls_in_constant_expressions [2020/02/09 20:10] – Document that method calls are compile errors tandrerfc: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 <tandre@php.net>   * Author: Tyson Andre <tandre@php.net>
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: https://wiki.php.net/rfc/calls_in_constant_expressions   * First Published at: https://wiki.php.net/rfc/calls_in_constant_expressions
  
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 call support is not part of this RFC (e.g. ''"MyClass::methodName"()'' or ''MyClass::methodName()''). (method calls continue to be fatal compile errors)+Method calls in constant expressions continue to be fatal errors (e.g. ''"MyClass::methodName"()'' or ''MyClass::methodName()'').
  
 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 options in this RFC for adding support to PHP+secondary Yes/No voting option is part of this RFCto allow calling any global function, not just those in the whitelist
-These voting options reflect the differing opinions on what a constant is meant to be.+That voting option reflects the differing opinions on what a constant is meant to be.
 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, any function (user-defined or internal) can be called. 
-The first option is to allow any function (user-defined or internal) to be called, +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 fatal compile error. 
 +This whitelist includes functions that are actually deterministic and without side effects, and don't depend on ini
 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 ''Error'' if they are called in a function expression (through the same mechanism that already forbids them in dynamic calls). will all throw an ''Error'' if they are called in a function expression (through the same mechanism that already forbids them in dynamic calls).
  
-Argument unpacking is allowed. (e.g. ''count(...[[1,2]])'')+Argument unpacking is allowed. (e.g. ''count(...[CONST_ARRAY])'')
  
 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 with calls will evaluate constant expressions containing function calls every time a class is instantiated or a function gets called without the parameter (unless a Throwable was thrown).+Parameter defaults of function/method/closure declarations (containing function calls) will get evaluated every time a function gets called without the parameter (unless a Throwable was thrown).
 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't throw. 
  
 ==== Handling functions not in the whitelist ==== ==== Handling functions not in the whitelist ====
Line 139: Line 133:
     For this reason, ''strlen()'', ''sprintf()'', and ''strpos()'' are omitted from the proposed whitelist. Those may be included in a followup RFC.     For this reason, ''strlen()'', ''sprintf()'', and ''strpos()'' are omitted from the proposed whitelist. Those may be included in a followup RFC.
  
-The list of functions is below:+The list of functions that would be included in the whitelist is below:
  
  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 **that are in the described whitelist** (Yes/No, Requires 2/3 majority)
  
 Secondary vote: Allow calling any global functions, without a whitelist limitation (Yes/No, Requires 2/3 majority for "Yes" to pass) Secondary vote: Allow calling any global functions, without a whitelist limitation (Yes/No, Requires 2/3 majority for "Yes" to pass)
rfc/calls_in_constant_expressions.txt · Last modified: 2020/03/04 14:01 by tandre