PHP Object-oriented __call handling error invocation technique

Source: Internet
Author: User


Before referring to __call, take a look at the test results of an instance to better understand the role of the __call method. Code on:

A system error occurs when a method that does not exist in the calling object is invoked, and the program exits and cannot continue. If you add a Magic method __call () to a class, the method is called automatically when a method that does not exist in the object is invoked, and the program can continue to execute downward. You can use the settings in the __call () method to prompt the user to invoke the method and the required argument list content does not exist. The __call () method requires two parameters, the first of which is to accept the method name of the nonexistent method, and to pass the argument list used in this nonexistent method to the second argument in the __call () method.

Code

The code is as follows Copy Code

<?php

This is a test class that has no attributes or methods in it.
Class Test
{
}


Object that produces a test class

$test =new test ();


Methods that do not exist in the calling object

$test->demo ("One", "two", "three");


The program will not execute here

echo "This is a test<br>";


?>

Run Result: Fatal error:call to undefined method Test::d Emo ()

We know that the result of the operation of the program throws out the error and is interrupted after the error is thrown in the running process, so that "$Person->say ();" This is not the right way to run again. Look at the code above to know that the person class does not have code errors, the wrong is wrong in the instantiation of the person class, a method that does not exist in the person class, such as run () and eat (), is invoked.

In the running of a program, an error such as the one thrown above is fatal and the entire program crashes. To handle this error while allowing the program to continue executing, we can add a magic method __call to the class to invoke the method automatically when it is not present in the object, and to allow the program to continue down execution.

The following will add a __call method based on the above code and debug the code as follows:
Code

The code is as follows Copy Code

<?php

This is a test class that has no attributes or methods in it.
Class Test
{

The method that is invoked automatically when the method is not saved, the first argument is the method name, and the second argument is the array parameter

function __call ($function _name, $args)
{

Print "The function you called: $function _name (parameter:";

Print_r ($args);

echo "does not exist! <br>n ";

}

}


Object that produces a test class

$test =new test ();


Methods that do not exist in the calling object

$test->demo ("One", "two", "three");


Program does not exit can be executed here

echo "This is a test<br>";

?>

Run Result:

The function you are calling: Run (parameter: Array ([0] => teacher)) does not exist!

The function you are calling: Eat (parameter: Array ([0] => child [1] => Apple)) does not exist!

Hello, wblog!.

The running result of this program no longer throws a fatal error, and the __call method is automatically invoked when the nonexistent method is invoked to catch the method that does not exist and to prompt the user, while invoking the existing method when the program executes normally.


Summary: Add a Magic Method __call to a class, which is called automatically when a method that does not exist in the object is invoked, and the program can continue to execute downward.

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.