The change is not to static properties themselves, but to the get_class_vars
function. The documented purpose of that function has always been to return the default values of all properties, so returning the current value for static properties in previous versions was technically a bug.
To get the current values of static properties, you can instead use the method ReflectionClass::getStaticProperties, e.g.
class Test {
public static $var = 0;
}
echo "Before:-\n";
echo "Default: "; var_dump( get_class_vars(Test::class) );
echo "Current: "; var_dump( (new ReflectionClass(Test::class))->getStaticProperties() );
test::$var = 42;
echo "After:-\n";
echo "Default: "; var_dump( get_class_vars(Test::class) );
echo "Current: "; var_dump( (new ReflectionClass(Test::class))->getStaticProperties() );
Running this across lots of PHP versions shows that for PHP versions back to 5.5 (and probably beyond, if the syntax of the example is tweaked) the output is (incorrectly):
Before:-
Default: array(1) {
["var"]=>
int(0)
}
Current: array(1) {
["var"]=>
int(0)
}
After:-
Default: array(1) {
["var"]=>
int(42)
}
Current: array(1) {
["var"]=>
int(42)
}
For PHP 8.1, it is (correctly):
Before:-
Default: array(1) {
["var"]=>
int(0)
}
Current: array(1) {
["var"]=>
int(0)
}
After:-
Default: array(1) {
["var"]=>
int(0)
}
Current: array(1) {
["var"]=>
int(42)
}
Alternatively, you could use get_class_vars
to get the names of the variables, and then "variable variable" syntax to get their current value.
Note that get_class_vars
returns both static and instance variables, so you need to first check that the static property exists before trying to fetch its value.
function get_current_static_vars(string $className): array {
$allVarNames = array_keys(get_class_vars($className));
$staticValues = [];
foreach ( $allVarNames as $varName ) {
if ( isset($className::$$varName) ) {
$staticValues[ $varName ] = $className::$$varName;
}
}
return $staticValues;
}
The above could easily be extended to emulate the old buggy get_class_vars
(which showed defaults for instance properties, but current values for static properties); or to show the current value of instance properties given a particular instance.