Php object-oriented OOP-_ call processing call errors

Source: Internet
Author: User
: 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.

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.