称为基类的成员函数,显式为statical

And*_*dre 8 php static overloading

我有一个带有魔术方法的基类__call并已_callStatic定义,因此可以处理对未声明的成员函数的调用.

当你同时拥有非静态和静态的两者时,似乎无法从派生类中调用静态操作符,因为静态操作符::并不隐含意味着staticparentor一起使用,或者在本例中使用的名称是基类.这是一个特殊的语法解释:http://php.net/manual/pl/keyword.parent.php

我想在这里做的是调用__callStaticwich失败的派生类,因为调用默认为非静态调用并由其处理__call.

如何在基类的成员函数上进行显式静态调用?

<?php

class MyBaseClass {

    public static function __callStatic($what, $args)
    {
        return 'static call';
    }

    public function __call($what, $args)
    {
        return 'non-static call';
    }

}

class MyDerivedClass extends MyBaseClass {

    function someAction()
    {
        //this seems to be interpreted as parent::Foo()
        //and so does not imply a static call
        return MyBaseClass::Foo(); //
    }

}

$bar = new MyDerivedClass();
echo $bar->someAction(); //outputs 'non-static call'

?>
Run Code Online (Sandbox Code Playgroud)

请注意,删除非静态__call方法会使脚本输出"静态调用",因为在未声明__callStatic__call会调用它.

НЛО*_*НЛО 3

为了避免这种行为,您可以使用一个空的代理类,该类在运行时 不parent链接:MyBaseClass

class MyBaseClass {

    public static function __callStatic($what, $args)
    {
        return 'static call ' . PHP_EOL;
    }

    public function __call($what, $args)
    {
        return 'dynamic call ' . PHP_EOL;
    }
}

class ProxyClass extends MyBaseClass {
    //"Empty" class
}

class MyDerivedClass extends MyBaseClass {

    function someAction()
    {
        return ProxyClass::Foo();
    }

}

$bar = new MyDerivedClass();
var_dump($bar->someAction()); //outputs 'static call'
Run Code Online (Sandbox Code Playgroud)

http://pastebin.com/7JMJUmXt