Php object-oriented _ call handling error calling skills

Source: Internet
Author: User
Before talking about _ call, let's take a look at the test results of an instance to better understand the function of the _ call method. when a method that does not exist in the call object is called, the system reports an error, then the program will return

Before mentioning _ call, let's take a look at the test results of an instance to better understand the functions of the _ call method. if a method does not exist in the called object, the system reports an error and the program cannot continue to run after it exits. If a "magic" method _ call () is added to the class, the method is automatically called when a method that does not exist in the object is called, and the program can continue to run down.

You can set the _ call () method to prompt that the method you call and the required parameter list content do not exist. The _ call () method requires two parameters. The first parameter is to call a method that does not exist, accept the name of the method that does not exist, and store the name of the method that does not exist, the list of parameters used form an array and pass it to the second parameter in the _ call () method.

The instance code is as follows:

  1. // This is a test class with no attributes or methods in it
  2. Class Test
  3. {
  4. }
  5.  
  6. // Generate a Test class object
  7. $ Test = new Test ();
  8.  
  9. // Call a method that does not exist in the object
  10. $ Test-> demo ("one", "two", "three ");
  11.  
  12. // The program will not be executed here
  13. Echo "this is a test
    ";
  14.  
  15. ?>

Running result: Fatal error: Call to undefined method Test: demo ()

We know that the program running result throws an error prompt, and the error is interrupted after an error is thrown during the running process, resulting in "$ Person-> say (); "This correct method cannot continue to run. Let's take a look at the code above. The Person class has no code error. if it is wrong, the method that does not exist in the Person class is called during the instantiation of the Person class, such as run () and eat ().

During program running, the error thrown above is fatal and the entire program will crash. To handle this error and keep the program running, we can add a magic method _ call to the class to automatically call a method that does not exist in the object, and the program can continue to run down.

Next we will add a _ call method based on the above code and debug it. the instance code is as follows:

  1. // This is a test class with no attributes or methods in it
  2. Class Test
  3. {
  4. // 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.
  5. Function _ call ($ function_name, $ args)
  6. {
  7. Print "the function you call: $ function_name (parameter :";
  8. Print_r ($ args );
  9. Echo "does not exist!
    N ";
  10. }
  11. }
  12.  
  13. // Generate a Test class object
  14. $ Test = new Test ();
  15.  
  16. // Call a method that does not exist in the object
  17. $ Test-> demo ("one", "two", "three ");
  18.  
  19. // The program will not exit and can be executed here
  20. Echo "this is a test
    ";
  21. ?>

Running result:

The function you called: run (parameter: Array ([0] => teacher) does not exist!

The function you call: eat (parameter: Array ([0] => child [1] => apple) does not exist!

Hello, wblog!

The running result of this program does not throw a fatal error. when a nonexistent method is called, The _ call method is automatically called to capture and send a prompt to the user, the program runs normally when an existing method is called.

Summary:Add a magic method _ call to the class. this method is automatically called when a method that does not exist in the object is called, and the program can continue to run down.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.