Home  >  Article  >  Backend Development  >  PHP overloaded method __call()

PHP overloaded method __call()

巴扎黑
巴扎黑Original
2016-11-11 17:53:071410browse


PHP overloaded method __call()

__call() method is used to monitor incorrect method calls.

__call() (Method overloading)

In order to avoid errors when the called method does not exist, you can use the __call() method to avoid it. This method will be automatically called when the called method does not exist, and the program will continue to execute.

Syntax:

function __call(string $function_name, array $arguments)

{

 …

}

This method has two parameters, the first parameter $function_name will be automatically received. The name of the existing method, the second $args receives multiple parameters of the non-existing method in the form of an array.

Add to the class:

function __call($function_name, $args)

{

echo "The function you called: $function_name(parameters:
";

var_dump($args );

echo ")Does not exist!";

}

When calling a method that does not exist (such as the test() method):

$p1=new Person();

$p1-> test(2,"test");

The output result is as follows:

The function you called: test(parameter:

array(2) {

[0]=>int(2)

[ 1]=>string(4) "test"

}

) does not exist!




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