Magic functions __call() for functions?

前端 未结 2 1232
北恋
北恋 2020-12-06 22:59

The magic function __call() in php are used in classes. Are there any similar magic function but for functions instead? Like __autoload() is for functions.

For examp

相关标签:
2条回答
  • 2020-12-06 23:06

    Nope, I don't think such a magic function exists.

    One workaround for this would be to put your functions into a static class, and add a __callStatic magic method to that 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 could do the same thing, but you would have to instantiate an object and use the __call magic method.

    $Func = new Func;
    $Func->random_func("hello!");
    
    0 讨论(0)
  • 2020-12-06 23:23

    No. Calling a function that doesn't exist will always result in a FATAL error.

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

    0 讨论(0)
提交回复
热议问题