rfc:arbitrary_static_variable_initializers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:arbitrary_static_variable_initializers [2023/03/16 11:40] – Fix syntax ilutov | rfc:arbitrary_static_variable_initializers [2023/05/24 18:29] (current) – Move to implemented ilutov | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2022-11-06 | * Date: 2022-11-06 | ||
* Author: Ilija Tovilo, tovilo.ilija@gmail.com | * Author: Ilija Tovilo, tovilo.ilija@gmail.com | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.3 | * Target Version: PHP 8.3 | ||
* Implementation: | * Implementation: | ||
Line 26: | Line 26: | ||
</ | </ | ||
- | The right hand side of the assignment < | + | The right hand side of the assignment < |
that it can't call functions, use parameters, amongst many other things. This limitation is hard to understand from a | that it can't call functions, use parameters, amongst many other things. This limitation is hard to understand from a | ||
user perspective. This RFC suggests lifting this restriction by allowing the static variable initializer to contain | user perspective. This RFC suggests lifting this restriction by allowing the static variable initializer to contain | ||
Line 70: | Line 70: | ||
</ | </ | ||
- | The static variable is overridden at compile time resulting in both statements | + | The static variable is overridden at compile time, resulting in both statements |
variable initializer. This is not useful or intuitive. The new implementation is not compatible with this behavior but | variable initializer. This is not useful or intuitive. The new implementation is not compatible with this behavior but | ||
would instead result in the first initializer to win. Instead of switching from one dubious behavior to another, | would instead result in the first initializer to win. Instead of switching from one dubious behavior to another, | ||
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. With this RFC this is no longer possible, as static variables may depend | variable if the function has never been called. With this RFC this is no longer possible, as static variables may depend | ||
Line 158: | Line 158: | ||
} | } | ||
- | function foo() { | + | function foo($y) { |
$x = new Foo(); | $x = new Foo(); | ||
- | static $x = 42; | + | static $x = $y; |
} | } | ||
try { | try { | ||
- | foo(); | + | foo(42); |
} catch (Exception) {} | } catch (Exception) {} | ||
Line 209: | Line 209: | ||
The answer is no. In this example it's clear that < | The answer is no. In this example it's clear that < | ||
- | Here's a quick explanation of how this is implemented: | + | Here's a quick explanation of how this is implemented: |
* Literals (strings, ints, bools, etc) | * Literals (strings, ints, bools, etc) | ||
Line 220: | Line 220: | ||
* Array literals | * Array literals | ||
* Magic constants (e.g. < | * Magic constants (e.g. < | ||
- | * Global constants | + | * Global constants |
- | * Class constants | + | * Class constants |
===== Vote ===== | ===== Vote ===== | ||
- | Voting starts | + | Voting starts |
As this is a language change, a 2/3 majority is required. | As this is a language change, a 2/3 majority is required. |
rfc/arbitrary_static_variable_initializers.txt · Last modified: 2023/05/24 18:29 by ilutov