PHP 8.1 – Inherited static variables

PHP 8.1 – Inherited static variables

static inherited dots

Local method static variables will now be inherited between multiple objects and even classes if base Class is used as parent Class. Previously each local static var was only related to the current type of Class.

This change would mean that a Class will have only one set of static variables per method, regardless of whether it is inherited:

class A {
    public static function counter() {
        static $i = 0;
        return ++$i;
    }
}
class B extends A {}
 
var_dump(A::counter()); // int(1)
var_dump(A::counter()); // int(2)
var_dump(B::counter()); // int(3)
var_dump(B::counter()); // int(4)

More about PHP 8.1

Read about all PHP 8.1 features and changes in here.