Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 example something like this

function __call($name, $arguments) {
    echo "Function $name says {$arguments[0]} ";
}
random_func("hello");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
330 views
Welcome To Ask or Share your Answers For Others

1 Answer

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). "
";

  }
 }

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!");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...