魔术函数__call()函数? [英] Magic functions __call() for functions?

查看:129
本文介绍了魔术函数__call()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

php中的魔术函数__call()用于类中。有没有类似的魔术功能,但功能,而不是?像__autoload()是用于函数的。



例如,类似这样的

  function __call($ name,$ arguments){
echoFunction $ name says {$ arguments [0]};
}
random_func(hello);


解决方案

不,我不认为这样的魔术功能存在。

对此的一种解决方法是将您的函数放入静态类中,并添加 />)。只有PHP 5.3,恐怕):

$ p $ class Func
{
/ ** PHP 5.3.0 * /
public static function __callStatic($ name,$ arguments)
{
//注意:$ name的值区分大小写。
echo调用静态方法'$ name'
。 implode(',',$ arguments)。 \\\
;




Func :: random_func(hello!);

对于PHP< 5.3,你可以做同样的事情,但你必须实例化一个对象并使用 __ call 魔术方法。

  $ Func = new Func; 
$ Func-> random_func(hello!);


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

解决方案

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

这篇关于魔术函数__call()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆