php魔术方法 __call
php的魔术方法是在调用不可见(不存在或无权限)的方法时出发的操作
我们写个PHP代码来解释下:
<?php
header("Content-type: text/html; charset=utf-8");
class cleey{
private function index(){}
/**
* [__call 魔术方法] 调用方法不存在或无权限时触发
* @param [type] $method 方法名
* @param [type] $arg 参数
*/
public function __call($method,$args){
echo '该方法不存在:',$method,'<br/>';
echo '参数为:',implode(',', $args),'<br/>------------------<br/>';
}
// 魔术方法__callStatic
// 同上,用于调用静态方法不存在
public static function __callStatic($method,$args){
echo '该静态方法不存在:',$method,'<br/>';
echo '参数为:',implode(',', $args),'<br/>------------------<br/>';
}
}
$cl = new cleey();
$cl->hello(1,2,3);
$cl->index('a','b');
/*
__call是调用不可见(不存在或无权限)的方法时,自动调用
$lisi->index('a','b');-----index()方法----> __call('index',array('a','b'))运行
*/
cleey::happy('好','开','森');
/*
__callStatic 是调用不可见的静态方法时,自动调用.
cleey::happy('好','开','森')----happy---> Human::__callStatic('happy',array('好','开','森'));
*/
?>
执行PHP的结果为:
该方法不存在:hello 参数为:1,2,3 ------------------ 该方法不存在:index 参数为:a,b ------------------ 该静态方法不存在:happy 参数为:好,开,森 ------------------