我有一个基类,定义了魔术方法__call和_callStatic,以便处理对未声明的成员函数的调用.
当你同时拥有非静态和静态的两者时,似乎无法从派生类中调用静态的,因为静态运算符::如果与父类一起使用,则不隐式表示静态,或者在本例中,基类的名称.这是一个特殊的语法:http://php.net/manual/pl/keyword.parent.php
我想在这里做的是调用__callStatic的派生类失败,因为调用默认为非静态调用并由__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' ?>
要避免此类行为,您可以使用空代理类,即在运行时不链接父代和MyBaseClass:
原文链接:https://www.f2er.com/php/136577.htmlclass 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'