php __call method is called when calling an undefined method, using syntax such as "$foo->__call('test', array(1, "2", 3.4, true))", also It is equivalent to "$foo->test(1, "2", 3.4, true)".
Recommended: "PHP Video Tutorial"
Usage of php magic method __call
__call is called when calling an undefined method.
In other words, if your test method is not defined, then the method name test will be passed in as the first parameter of __call, and the parameters of test will be loaded into the array as the first parameter of __call. Two parameters are passed in.
So when you call $foo->test(1, "2", 3.4, true), it is actually equivalent to calling $foo->__call('test', array(1, " 2", 3.4, true)).
__call method is triggered when calling a class method, for example:
<?php class google{ public function search(){ //TODO } public function __call($method, $parameters){ //这里的method便是对应的方法,即"->"后面的字符串,$parameters是通过这个方法传过来的参数 } } $google = new google(); $keyword = 'VR'; $google->search($keyword); //当调用当前对象不存在的方法时,会转向__call $google->operate();
Using __call, you can do some encapsulation to call other objects and methods.
The above is the detailed content of How to use php __call method. For more information, please follow other related articles on the PHP Chinese website!