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 19:49] – ilutov | rfc:arbitrary_static_variable_initializers [2023/03/16 11:07] – Fix reflection example 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 $cond; | + | 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) " |
</ | </ | ||
rfc/arbitrary_static_variable_initializers.txt · Last modified: 2023/05/24 18:29 by ilutov