🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### __call() 当调用一个不可访问的对象方法(非静态方法),会自动的执行该魔术方法! 需要两个参数 : 参数一:方法名,string型 参数二:array型,因为参数的个数不确定,只能把所有的参数都放到一个数组里面. ### 默认行为 ~~~ class Person{ } $p = new Person(); $p->show(); ~~~ ~~~ Fatal error: Uncaught Error: Call to undefined method Person::show() //报错 ~~~ ### 重写该方法 ~~~ class Person{ public function __call($name, $arguments) //这两个形参必须要填写 { var_dump($arguments); } } $p = new Person(); $p->show(10,20); ~~~ ~~~ array(2) { [0]=> int(10) [1]=> int(20) } ~~~ 或 ~~~ class Person{ private function show($name, $age) { echo $name, $age; } public function __call($name, $arguments) { $this->$name($arguments[0], $arguments[1]); } } $p = new Person(); $p->show(10,20); ~~~ ~~~ 10 20 ~~~