Why __call is called instead of __callStatic

I tried passing this question to SO, but still not getting it.

<?php

class A {
    public function __call($method, $parameters) {
        echo "I'm the __call() magic method".PHP_EOL;
    }

    public static function __callStatic($method, $parameters) {
        echo "I'm the __callStatic() magic method".PHP_EOL;
    }
}

class B extends A {
    public function bar() {
        A::foo();
    }

    public function foo() {
        parent::foo();
    }
}

(new B)->bar();
(new B)->foo();

From what I understand, the function barcalls the method fooin the class Astatically, but the method foocalls the method using an instance Athat is the parent element B, I expect this to give me:

I'm the __callStatic() magic method
I'm the __call() magic method

But apparently I get:

I'm the __call() magic method
I'm the __call() magic method
+4
source share
2 answers

From the corresponding issue :

... A::foo() . , foo() ($this , , ), .

foo() - static, , :

class A {
    public function __call($method, $parameters) {
        echo "I'm the __call() magic method $method".PHP_EOL;
    }

    public static function __callStatic($method, $parameters) {
        echo "I'm the __callStatic() magic method $method".PHP_EOL;
    }
}

class B extends A {
    public static function foo() { // <-- static method
        parent::foo();
    }
}

(new B)->foo();

- __callStatic() foo

+3

A::foo(), , . B::bar() $this, foo, A, B A, , __call. - , .

+3

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


All Articles