Re: [RFC] Static variables in inherited methods
I can understand that some people coming from C++, Java may be shocked by
the behavior of a static variable in a static function.
And the need to improve things.
Maybe can you add *virtual static* keywords as a replacement for old
behavior in your RFC <https://wiki.php.net/rfc/static_variable_inheritance>
. You would satisfy me.
class A {
static function Counter() {
static $count = 0;
virtual static $i = 1;
return [++$count, $i++];
}
}
class B extends A {}
var_dump(A::Counter()); // array(int(1), int(1))
var_dump(A::Counter()); // array(int(2), int(2))
var_dump(B::Counter()); // array(int(3), int(1))
var_dump(B::Counter()); // array(int(4), int(2))
A::Counter::$count = 665; // Great Feature
A::Counter::$i = 0; //Initialize
B::Counter::$i = 0; //Reset
In C Poo we could get :
/*header*/
typedef struct _A {// Instance class A
} A;
typedef struct _AClass {// vtable class A
int counter_i;// php: virtual static $i
void (*counter)(void);// php: public static function counter(){}
} AClass;
typedef struct _B {// Instance class B A parent_instance;
} B;
typedef struct _BClass {// vtable class B AClass parent_class;// B
contains its own virtual static variable $i
} BClass;
/*source*/
int A_counter_count = 0;// What did you expect ?
//...
A_counter_count = 665;// php: A::Counter::$count=665;
A_class_entity.i = 0;// php: A::Counter::$i=0;
B_class_entity.i = 0;// php: B::Counter::$i=0;
Best regards,
Serge
Le mar. 23 févr. 2021 à 15:02, Nikita Popov <nikita.ppv@gmail.com> a écrit :
> Hi internals,
>
> While looking into various issues related to static variable handling, I've
> become increasingly convinced that our handling of static variables in
> inherited methods is outright buggy. However, it's also long-standing
> behavior, so I've put up an RFC:
>
> https://wiki.php.net/rfc/static_variable_inheritance
>
> Regards,
> Nikita
>
Thread (11 messages)