rfc:arbitrary_static_variable_initializers
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:arbitrary_static_variable_initializers [2022/11/06 20:26] – Fix code error ilutov | rfc:arbitrary_static_variable_initializers [2023/03/16 11:39] – Explain what is comp-time evaluable ilutov | ||
---|---|---|---|
Line 87: | Line 87: | ||
< | < | ||
current values. Currently, PHP automatically evaluates the underlying constant expression and initializes the static | current values. Currently, PHP automatically evaluates the underlying constant expression and initializes the static | ||
- | variable if the function has never been called, since the initializer cannot depend on any runtime values of the | + | variable if the function has never been called. |
- | function. This is no longer possible | + | on values that are only known at runtime. Instead, the compiler will //attempt// to resolve the constant expression at |
- | < | + | compile time. If successful, the value will be embedded in the static variables table. Otherwise it will be initialized |
- | and assigning to the static | + | to < |
+ | be reflectable | ||
<code php> | <code php> | ||
Line 104: | Line 105: | ||
foo(2); | foo(2); | ||
var_dump((new ReflectionFunction(' | var_dump((new ReflectionFunction(' | ||
- | // 2 | + | // 1 |
</ | </ | ||
From the example above, it becomes more obvious why the initializer < | From the example above, it becomes more obvious why the initializer < | ||
calling the function. | calling the function. | ||
- | |||
- | //Side note:// It's been suggested that expressions that can be evaluated constantly continue to do so. This would mean that some expressions in < | ||
===== Other semantics ===== | ===== Other semantics ===== | ||
Line 184: | Line 183: | ||
function foo($i) { | function foo($i) { | ||
static $x = $i < 3 ? foo($i + 1) : ' | static $x = $i < 3 ? foo($i + 1) : ' | ||
- | var_dump($a); | + | var_dump($x); |
return $i; | return $i; | ||
} | } | ||
foo(1); | foo(1); | ||
- | // string(4) " | + | // string(4) " |
- | // string(4) " | + | // string(4) " |
- | // string(4) " | + | // string(4) " |
foo(5); | foo(5); | ||
- | // string(4) " | + | // string(4) " |
</ | </ | ||
+ | |||
+ | ==== What initializers are known at compile-time? | ||
+ | |||
+ | In the discussion the question arose whether static variables depending on other static variables are known at compile time. | ||
+ | |||
+ | <code php> | ||
+ | function foo() { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The answer is no. In this example it's clear that < | ||
+ | |||
+ | Here's a quick explanation of how this is implemented: | ||
+ | |||
+ | * Literals (strings, ints, bools, etc) | ||
+ | * Binary operations | ||
+ | * Binary comparisons | ||
+ | * Unary operations | ||
+ | * Coalesce operator | ||
+ | * Ternary operator | ||
+ | * Array access (< | ||
+ | * Array literals | ||
+ | * Magic constants (e.g. < | ||
+ | * Global constants | ||
+ | * Class constants | ||
===== Vote ===== | ===== Vote ===== |
rfc/arbitrary_static_variable_initializers.txt · Last modified: 2023/05/24 18:29 by ilutov