码迷,mamicode.com
首页 > Web开发 > 详细

PHP 重载方法 __call()

时间:2017-10-09 17:44:59      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:new   int   调用   class   arguments   cti   str   dump   method   

__call() 方法用于监视错误的方法调用。

__call()(Method overloading)

为了避免当调用的方法不存在时产生错误,可以使用 __call() 方法来避免。该方法在调用的方法不存在时会自动调用,程序仍会继续执行下去。

语法:

function __call(string $function_name, array $arguments)
{
    ......
}

该方法有两个参数,第一个参数 $function_name 会自动接收不存在的方法名,第二个 $args 则以数组的方式接收不存在方法的多个参数。

在类里面加入:

function __call($function_name, $args)
{
    echo "你所调用的函数:$function_name(参数:<br />";
    var_dump($args);
    echo ")不存在!";
}

当调用一个不存在的方法时(如 test() 方法):

$p1=new Person();
$p1->test(2,"test");

输出的结果如下:

你所调用的函数:test(参数:
array(2) {
    [0]=>int(2)
    [1]=>string(4) "test"
}
)不存在!

PHP 重载方法 __call()

标签:new   int   调用   class   arguments   cti   str   dump   method   

原文地址:http://www.cnblogs.com/keta/p/7641857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!