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/07 19:41] – Fix typo theodorejb | rfc:arbitrary_static_variable_initializers [2023/03/16 12:31] – ilutov | ||
---|---|---|---|
Line 85: | Line 85: | ||
==== ReflectionFunction:: | ==== ReflectionFunction:: | ||
- | < | + | < |
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 196: | Line 195: | ||
// 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 (that are known at compile time) | ||
+ | * Class constants (that are known at compile time) | ||
===== Vote ===== | ===== Vote ===== |
rfc/arbitrary_static_variable_initializers.txt · Last modified: 2023/05/24 18:29 by ilutov