


PHP object-oriented __call handling error calling skills_PHP tutorial
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 | ||||
"; ?> |
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
The code is as follows
|
Copy code
|
||||
//This is a test class, there are no attributes and methods in it |
Class Test
//Generate an object of Test class $test=new 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...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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.

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
