Call a member function of a base class explicitly statically

I have a base class with the specified magic methods __call and _callStatic , so that calls to undeclared member functions are handled.

If you have both dynamic and static, it seems impossible to call static from a derived class, because the static :: operator does not imply a static value if used with parent or, as in this case, the name of the base class. This is the special syntax described here: http://php.net/manual/pl/keyword.parent.php

What I want to do here is a derived class for calling __callStatic , which fails because the default is to use a dynamic call and handle __call .

How can I make an explicit static call to a member function of a base class?

 <?php class MyBaseClass { public static function __callStatic($what, $args) { return 'static call'; } public function __call($what, $args) { return 'dynamic 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 'dynamic call' ?> 

Note that removing the dynamic __call method makes the script output a β€œstatic call” because __callStatic is __callStatic when __call not declared.

+8
php static overloading
source share
1 answer

To avoid this behavior, you can use an empty proxy class that does not bind parent and MyBaseClass at runtime:

 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' 

http://pastebin.com/7JMJUmXt

+3
source share

All Articles