Why does PHP assign context to a call to a static method and not give an E_STRICT notification?

I came across very strange behavior in PHP5.4 (also present in 5.5). Basically, I am calling the non-static method statically, and I am not getting the E_STRICT error, where I definitely should get it.

<?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 a call from the context of the object, 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 parent B):

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

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

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

, (?) ? :)

0
1

, . , $this B - , , PHP , , , , ( ). :: - , , , , , parent::method() . .

:

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 

B, , foo() .

+1

Source: https://habr.com/ru/post/1657034/


All Articles