On Thu, Feb 25, 2021 at 9:48 AM Aleksander Machniak <a...@alec.pl> wrote:

> On 23.02.2021 15:01, Nikita Popov wrote:
> > 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
>
> What about non-static methods with static vars inside? I think it should
> be mentioned in the RFC, that it does not matter. Or the examples should
> not use a static method, for clarity.
>

Thanks for the suggestion. I've explicitly mentioned that the behavior is
independent of whether the method is static, and added an example.


> I'm not sure I would agree with the Traits behavior being consistent.


Yes, this part is not so clear cut. I think that intuitively, this is the
correct behavior because it makes static variables work the same way as
static properties. An additional consideration is that it's possible to use
trait methods multiple times in the same class:

<?php

trait T {
    public static function counter() {
        static $i = 0;
        return ++$i;
    }
}
class A {
    use T {
        counter as counter1;
        counter as counter2;
        counter as counter3;
    }
}
var_dump(A::counter1());
var_dump(A::counter2());
var_dump(A::counter3());

I think it would be rather odd if A::counter1(), A::counter2(),
A::counter3() all shared the same static variables, despite being distinct
methods with distinct names.

Regards,
Nikita

Reply via email to