rfc:arbitrary_static_variable_initializers

This is an old revision of the document!


PHP RFC: Arbitrary static variable initializers

Proposal

PHP allows declaring static variables in all functions. Static variables outlive the function call and are shared across future execution of the function.

function foo() {
    static $i = 1;
    echo $i++, "\n";
}
 
foo();
// 1
foo();
// 2
foo();
// 3

The right hand side of the assignment static $i = 1; must currently be a constant expressions. This means 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 arbitrary expressions.

function bar() {
    echo "bar() called\n";
    return 1;
}
 
function foo() {
    static $i = bar();
    echo $i++, "\n";
}
 
foo();
// bar() called
// 1
foo();
// 2
foo();
// 3

Backwards incompatible changes

Redeclaring static variables

Currently, redeclaring static variables is allowed, although the semantics are very questionable.

function foo() {
    static $x = 1;
    var_dump($x);
    static $x = 2;
    var_dump($x);
}
 
foo();
// 2
// 2

The static variable is overridden at compile time resulting in both statements to refer to the same underlying static 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, redeclaring static variables is disallowed in this RFC and results in a compile time error.

function foo() {
    static $x = 1;
    static $x = 2;
}
// Fatal error: Duplicate declaration of static variable $x

ReflectionFunction::getStaticVariables()

ReflectionFunction::getStaticVariables() 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 variable if the function has never been called, since the initializer cannot depend on any runtime values of the function. This is no longer possible with arbitrary initializers. Instead, static variables in ReflectionFunction::getStaticVariables() will be initialized to null. After executing the function and assigning to the static variables the contents of the variables will be reflectable.

function foo($initialValue) {
    static $x = $initialValue;
}
 
var_dump((new ReflectionFunction('foo'))->getStaticVariables()['x']);
// NULL
foo(1);
var_dump((new ReflectionFunction('foo'))->getStaticVariables()['x']);
// 1
foo(2);
var_dump((new ReflectionFunction('foo'))->getStaticVariables()['x']);
// 2

From the example above, it becomes more obvious why the initializer $initialValue cannot be evaluated before 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 getStaticVariables 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

Exceptions during initialization

An initializer might throw an exception. In that case, the static variable remains uninitialized and the initializer will be called again in the next execution.

function bar($throw) {
    echo "bar() called\n";
    if ($throw) throw new Exception();
    return 42;
}
 
function foo($throw) {
    static $x = bar($throw);
}
 
try {
    foo(true);
} catch (Exception) {}
// bar() called
var_dump((new ReflectionFunction('foo'))->getStaticVariables()['x']);
// NULL
 
foo(false);
// bar() called
var_dump((new ReflectionFunction('foo'))->getStaticVariables()['x']);
// int(42)
 
foo(true);
// bar is not called anymore

Destructor

When the static variable declaration overwrites an existing local variable that contains an object with a destructor that throws an exception, the assignment of the static variable is guaranteed to occur before the exception is thrown. This is analogous to assignments to regular variables.

class Foo {
    public function __destruct() {
        throw new Exception();
    }
}
 
function foo() {
    $x = new Foo();
    static $x = 42;
}
 
try {
    foo();
} catch (Exception) {}
 
var_dump((new ReflectionFunction('foo'))->getStaticVariables()['x']);
// 42

Recursion

The static variable declaration only runs the initializer if the static variable has not been initialized. When the initializer calls the current function recursively this check will be reached before the function has been initialized. This means that the initializer will be called multiple times. Note though that the assignment to the static variable still only happens once. This is a somewhat technical limitation where the opcode needs to release two values that could both execute user code and thus throw exceptions. Not reassigning the value avoids this issue. However, I cannot imagine a useful scenario for recursive static variable initializers, so semantics here are unlikely to matter.

function foo($i) {
    static $x = $i < 3 ? foo($i + 1) : 'Done';
    var_dump($x);
    return $i;
}
 
foo(1);
// string(4) "Done", $i = 3
// string(4) "Done", $i = 2
// string(4) "Done", $i = 1
 
foo(5);
// string(4) "Done", $i = 5, initializer not called

Vote

Voting starts ??? and ends ???.

As this is a language change, a 2/3 majority is required.

Allow arbitrary static variable initializers in PHP 8.3?
Real name Yes No
alcaeus (alcaeus)  
colinodell (colinodell)  
cpriest (cpriest)  
crell (crell)  
cschneid (cschneid)  
galvao (galvao)  
girgias (girgias)  
ilutov (ilutov)  
imsop (imsop)  
kocsismate (kocsismate)  
mcmic (mcmic)  
nicolasgrekas (nicolasgrekas)  
nielsdos (nielsdos)  
ocramius (ocramius)  
pierrick (pierrick)  
ramsey (ramsey)  
sebastian (sebastian)  
seld (seld)  
sergey (sergey)  
svpernova09 (svpernova09)  
thekid (thekid)  
theodorejb (theodorejb)  
timwolla (timwolla)  
villfa (villfa)  
weierophinney (weierophinney)  
Final result: 25 0
This poll has been closed.
rfc/arbitrary_static_variable_initializers.1667766774.txt.gz · Last modified: 2022/11/06 20:32 by ilutov