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!