__Call () magic functions for functions?

The magic function __call () in php is used in classes. Is there any similar magic function besides functions? Like __autoload () for functions.

For example, something like this

function __call($name, $arguments) { echo "Function $name says {$arguments[0]} "; } random_func("hello"); 
+2
source share
2 answers

No, I don’t think such a magic function exists.

A workaround for this would be to turn your functions into a static class and add the __callStatic magic method for this class (> PHP 5.3 only, I'm afraid):

 class Func { /** As of PHP 5.3.0 */ public static function __callStatic($name, $arguments) { // Note: value of $name is case sensitive. echo "Calling static method '$name' " . implode(', ', $arguments). "\n"; } } Func::random_func("hello!"); 

For PHP <5.3, you can do the same, but you need to instantiate the object and use the magic __call method.

 $Func = new Func; $Func->random_func("hello!"); 
+3
source

Not. Calling a function that does not exist will always result in a FATAL error.

** Maybe the zend extension can intercept this with fcall_begin_handler , but I'm not sure.

+3
source

All Articles