__call('test', array(1, "2", 3.4, true))", which is equivalent to "$foo- >test(1, "2", 3.4, true)"."/> __call('test', array(1, "2", 3.4, true))", which is equivalent to "$foo- >test(1, "2", 3.4, true)".">

Home  >  Article  >  Backend Development  >  How to use php __call method

How to use php __call method

藏色散人
藏色散人Original
2020-08-28 09:57:482387browse

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)".

How to use php __call method

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:

"后面的字符串,$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn