rfc:arbitrary_static_variable_initializers

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:arbitrary_static_variable_initializers [2022/11/06 20:27] – Another one ilutovrfc:arbitrary_static_variable_initializers [2023/03/16 11:07] – Fix reflection example ilutov
Line 87: Line 87:
 <php>ReflectionFunction::getStaticVariables()</php> can be used to inspect a functions static variables and their <php>ReflectionFunction::getStaticVariables()</php> can be used to inspect a functions static variables and their
 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. With this RFC this is no longer possible, as static variables may depend 
-functionThis is no longer possible with arbitrary initializers. Instead, static variables in +on values that are only known at runtime. Instead, the compiler will //attempt// to resolve the constant expression at 
-<php>ReflectionFunction::getStaticVariables()</php> will be initialized to <php>null</php>. After executing the function +compile time. If successful, the value will be embedded in the static variables table. Otherwise it will be initialized 
-and assigning to the static variables the contents of the variables will be reflectable.+to <php>null</php>. After executing the function and assigning to the static variable the contents of the variable will 
 +be reflectable through <php>ReflectionFunction::getStaticVariables()</php>.
  
 <code php> <code php>
Line 104: Line 105:
 foo(2); foo(2);
 var_dump((new ReflectionFunction('foo'))->getStaticVariables()['x']); var_dump((new ReflectionFunction('foo'))->getStaticVariables()['x']);
-// 2+// 1
 </code> </code>
  
 From the example above, it becomes more obvious why the initializer <php>$initialValue</php> cannot be evaluated before From the example above, it becomes more obvious why the initializer <php>$initialValue</php> cannot be evaluated before
 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 <php>getStaticVariables</php> are evaluated and some are not. The upside is that this would avoid the backward incompatibility. I will check if this is technically feasible. 
  
 ===== Other semantics ===== ===== Other semantics =====
Line 189: Line 188:
  
 foo(1); foo(1);
-// string(4) "Done" // $i = 3 +// string(4) "Done"$i = 3 
-// string(4) "Done" // $i = 2 +// string(4) "Done"$i = 2 
-// string(4) "Done" // $i = 1+// string(4) "Done"$i = 1
  
 foo(5); foo(5);
-// string(4) "Done" // $i = 5, initializer not called+// string(4) "Done"$i = 5, initializer not called
 </code> </code>
  
rfc/arbitrary_static_variable_initializers.txt · Last modified: 2023/05/24 18:29 by ilutov