How to use php __call method

藏色散人
Release: 2023-03-05 11:16:01
Original
2514 people have browsed it

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:

<?php
class google{
    public function search(){
        //TODO
    }
    public function __call($method, $parameters){
        //这里的method便是对应的方法,即"->"后面的字符串,$parameters是通过这个方法传过来的参数
    }
}
 
$google = new google();
$keyword = &#39;VR&#39;;
$google->search($keyword);
//当调用当前对象不存在的方法时,会转向__call
$google->operate();
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template