xuesn 发表于 2017-3-23 12:50:56

php方法重载

class Test {
function __call($name , $args) {
$method = $name."__".count($args);
return call_user_func_array(array($this , $method) , $args);
}
function add__2($a , $b) {
echo ($a + $b)."<br />";
}
function add__3($a , $b , $c) {
echo ($a + $b + $c)."<br />";
}
}
$test = new Test();
$test -> add(1 , 2);
$test -> add(1 , 2 , 3);

看文档说的是__call在类中不存在客户调用方法的时候被执行 , 在本类中的大致意思就是计算参数的个数 , 然后用调用函数名+"__"+参数个数的形式 , 调用相应的函数。
add(1 , 2); 就是调用add_2(1 ,2 );add(1 , 2 , 3);就是调用add_3(1 , 2 , 3);
页: [1]
查看完整版本: php方法重载