Php object-oriented _ call handling error call tips _ PHP Tutorial

Source: Internet
Author: User
Php object-oriented _ call handling error calling skills. This article will introduce you to the php object-oriented _ call processing error calling skills. call is rarely used in the past. let's test it today. Before talking about _ call, let's first introduce this article to you about how to handle error call in php object-oriented _ call. call is rarely used in the past, let's test it today.


Before talking about _ call, let's take a look at the test results of an instance to better understand the role of The _ call method. Code:

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.

Code

The code is as follows:

// 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
";


?>

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 code is as follows:
Code

The code is as follows:

// 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!
N ";

}

}


// 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
";

?>

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.


To sum up, add a magic method _ call to the class. this method is automatically called when a method that does not exist in the calling object, and the program can continue to run down.

Bytes. Before _ call is mentioned, first come...

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.