Home >Backend Development >PHP Tutorial >PHP object-oriented __call handling error calling skills_PHP tutorial
This article will introduce to you the techniques for handling error calls in PHP object-oriented __call. Call was rarely used in the past, so I will test it for you today.
Before mentioning __call, let’s look at the test results of an example to better understand the role of the __call method. Above code:
When calling a method that does not exist in the object, a system error will occur, and then the program will exit and cannot continue execution. If you add a "magic" method __call() to the class, the method will be automatically called when calling a method that does not exist in the object, and the program can continue to execute. You can prompt the user that the method called and the required parameter list do not exist through the settings in the __call() method. The __call() method requires two parameters. The first parameter is to accept the method name of the non-existent method when calling a method that does not exist, and pass the parameter list used in the non-existent method to form an array and pass it to __call. () second parameter in the method.
Code
The code is as follows | Copy code | ||||
"; ?> |
Run result: Fatal error: Call to undefined method Test::demo()
We know that the running result of the program throws an error message. After the error is thrown during the running process, it has been interrupted, so that the correct method of "$Person->say();" cannot be continued. run. If you look at the above code, you will know that there is no code error in the Person class. The mistake is that in the process of instantiating the Person class, methods that do not exist in the Person class are called, such as run() and eat().
During the running of the program, the error thrown above is fatal and the entire program will crash. In order to handle this error while allowing the program to continue executing, we can add a magic method __call to the class to automatically call the method when calling a method that does not exist in the object, and allow the program to continue executing.
Based on the above code, we will add an additional __call method and debug it. The code is as follows:
Code
The code is as follows
|
Copy code
|
||||
//This is a test class, there are no attributes and methods in it |
Class Test
//Generate an object of Test class $test=new Test();