Under what conditions does PHP 7's self refer to the base class?

Viewed 123

As noted on Reddit's LOL PHP sub, PHP 7 may use either the extended class or the base class when referring to self, in contrast to PHP 5 which always refers to the extended class.

<?php

class Foo {
    const A = "FooA";
    const B = self::A . self::C;
    const C = "FooC";

}

class Bar extends Foo {
    const A = "BarA";
    const C = "BarC";
}

var_dump(Bar::B);

Try it online

PHP 5

string(8) "BarABarC"

PHP 7

string(8) "FooABarC"

The behaviour of PHP 7 is particularly worrisome as there does not seem to be any simple rule to know when self refers to the base class or the extended class. What are rules for determining to which class self will refer in PHP 7?

2 Answers
Related