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

Jul 13, 2016 am 10:49 AM
c php one time introduce about exist deal with object Skill article transfer mistake For

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles