Home > Backend Development > PHP Tutorial > PHP object-oriented __call handling error calling skills_PHP tutorial

PHP object-oriented __call handling error calling skills_PHP tutorial

WBOY
Release: 2016-07-13 10:49:43
Original
1021 people have browsed it

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
 代码如下 复制代码

 

//这是一个测试的类,里面没有属性和方法
class Test
{
}


//产生一个Test类的对象

$test=new Test();


//调用对象里不存在的方法

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


    //程序不会执行到这里

    echo "this is a test
";


?>

<🎜> //This is a test class, there are no attributes and methods in it <🎜> Class Test <🎜> { <🎜> }<🎜> <🎜><🎜> //Generate an object of Test class<🎜> <🎜> $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
"; ?>

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

Class Test {
The code is as follows
 代码如下 复制代码

 

    //这是一个测试的类,里面没有属性和方法
    class Test
    {

        //调用不存的方法时自动调用的方法,第一个参数为方法名,第二个参数是数组参数

        function __call($function_name, $args)
        { 

            print "你所调用的函数:$function_name(参数:"; 

            print_r($args); 

            echo "不存在!
n";

        }

    }


    //产生一个Test类的对象

    $test=new Test();


    //调用对象里不存在的方法

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


    //程序不会退出可以执行到这里

    echo "this is a test
";

?>

运行结果:

你所调用的函数:run(参数:Array ( [0] => teacher ) )不存在!

你所调用的函数:eat(参数:Array ( [0] => child [1] => apple ) )不存在!

Hello, wblog!

Copy code

//This is a test class, there are no attributes and methods in it
//Method that is automatically called when calling a non-existing method. The first parameter is the method name, and the second parameter is the array parameter

function __call($function_name, $args) print "The function you called: $function_name(parameter: "; print_r($args); echo "Does not exist!
n"; } }
//Generate an object of Test class $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
"; ?> Run result: The function you called: run (parameter: Array ( [0] => teacher ) ) does not exist! The function you called: eat (parameter: Array ( [0] => child [1] => apple ) ) does not exist! Hello, wblog! This time the running result of the program no longer throws a fatal error. When calling a method that does not exist, the __call method is automatically called to capture the non-existing method and prompt the user. When calling an existing method, the program executes normally. . Summary: Add a magic method __call to the class. This method will be automatically called when calling a method that does not exist in the object, and the program can continue to execute. http://www.bkjia.com/PHPjc/632688.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632688.htmlTechArticleThis article will introduce to you the techniques for handling error calls in php object-oriented __call. Call was very common in the past. Use it sparingly and let me test it for you today. Before mentioning __call, let’s first...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template