سؤال

I noticed that each of the examples below will echo __call.

class A {
    public function __construct() {
        $this->something();
        self::something();
        call_user_func(array("self", "something"));
        forward_static_call(array("self", "something"));
        A::something();
        call_user_func(array(__CLASS__, "something"));
        forward_static_call(array(__CLASS__, "something"));
    }

    public function __call($name, $arguments) {
        echo __FUNCTION__ . "<br />";
    }

    public static function __callStatic($name, $arguments) {
        echo __FUNCTION__ . "<br />";
    }

}

new A();

Is it possible to invoke the __callStatic method from inside object context, even when a __call magic method is present in the class?

I find that call_user_func(array(__CLASS__, "__callStatic"), array("method"), array()); is somewhat ugly.

هل كانت مفيدة؟

المحلول

__callStatic() is triggered when invoking inaccessible methods in a static context.

But when you did foo::bar() from a non-static context (in your code, __construct method is non-static context) will be a non-static call, unless the function is explicitly defined as static.

So if you really want to use __callStatic() in the __construct method, you could use it directly.

public function __construct() {
    self::__callStatic('something', array());
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top