In first case, you have no object context since you're calling your non-static method from outer space. But in second case, you have object context since $this
will refer to instance of B
- and, therefore, PHP will find that object context exists, and, therefore, it is a non-static call of non-static method (everything is ok). If you're hesitating about call via ::
- then, I think, you should remind that, for example, parent::method()
is a valid call. I.e. referring way is not the problem here.
To be more specific:
class A
{
public function foo()
{
echo('foo called, class: '. get_class($this).PHP_EOL);
}
}
class B extends A
{
public function __construct()
{
A::foo();
}
}
$b=new B(); //foo called, class: B
so you'll see class B
as expected since foo()
was inherited.