: This article mainly introduces php object-oriented OOP-_ call to handle call errors. For more information about PHP tutorials, see. In program development, if the internal method of the object is called when the object does not exist, the program will fail and the program cannot continue to execute after exiting. Then, when the program calls a method that does not exist inside the object, it prompts that the method we call and the parameters we use do not exist, but the program can continue to execute, in this case, we need to use the "_ call ()" method that is automatically called when a method that does not exist is called ()".
// This is a test class with no attributes or methods in it
Class Test
{
}
// Generate a Test class object
$ Test = new Test ();
// Call a method that does not exist in the object
$ Test-> demo ("one", "two", "three ");
// The program will not be executed here
Echo "this is a test
";
?>
In the preceding example, the following error occurs. Generally, the program cannot be executed;
Fatal error: Call to undefined method Test: demo ()
Next, we add the "_ call ()" method. this method has two parameters. The first parameter is used to automatically call the _ call () method when a nonexistent method is called, pass the method name of this nonexistent method to the first parameter, and the second parameter is to pass multiple parameters of this method in the form of an array.
// This is a test class with no attributes or methods in it
Class Test
{
// The method automatically called when a non-stored method is called. The first parameter is the method name, and the second parameter is the array parameter.
Function _ call ($ function_name, $ args ){
Print "the function you call: $ function_name (parameter :";
Print_r ($ args );
Echo ") does not exist!
";
}
}
// Generate a Test class object
$ Test = new Test ();
// Call a method that does not exist in the object
$ Test-> demo ("one", "two", "three ");
// The program will not exit and can be executed here
Echo "this is a test
";
?>
The output result of the preceding example is:
The function you called: demo (parameter: Array ([0] => one [1] => two [2] => three) does not exist!
This is a test
The above content is derived from "fried peanuts". thank you for sharing it.
The above introduces php object-oriented OOP-_ call for handling call errors, including some content, and hopes to help those who are interested in PHP tutorials.