Why does PHP assigns context to a static method ca

2019-08-30 21:14发布

问题:

I ran into some very strange behaviour on PHP5.4 (also present in 5.5). Basically, I am calling a non-static method statically and I am not getting an E_STRICT error where I definitely should be getting one.

<?php
error_reporting(E_ALL);
class A
{
    public function iAmNotStatic() {}
}

Now, if I do this:

A::iAmNotStatic();

Then I get the error as expected Strict standards: Non-static method A::iAmNotStatic() should not be called statically.

And also, if I make the call from object context, I also get the same error (as expected)

class B 
{
    public function __construct() {
        A::iAmNotStatic();
    }
}

$b = new B(); // error here, as expected

However, if I do this (assign A to be the parent of B):

class B extends A
{
    public function __construct() {
        A::iAmNotStatic();
    }
}

$b = new B(); // no error 

Then PHP decides that "no problem, I have an object ($b) with the same parent class (A), let's just make it the context for iAmNotStatic".

So, is this a feature or a bug and what might be the purpose of this confusing (undocumented?) behaviour? Thanks :)

回答1:

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.