


PHP implements a method to force the callback type to be specified based on Callable
Jun 01, 2018 pm 02:14 PMThis article mainly introduces the method of forcing PHP Callable to specify the callback type. It is very good and has reference value. Friends who need it can refer to it
If a method needs to accept a callback method as a parameter, we You can write like this
1 2 3 4 5 6 7 8 9 |
|
Output:
do sth callback
But we cannot be sure whether the callback method can be called, so A lot of extra work needs to be done to check whether this callback method can be called.
Is there any better way to determine whether the callback method is callable?
We can use callable to force the specified parameter to be a callback type, which ensures that the callback method must be callable.
For example, the callback method is a non-existent method
1 2 3 4 5 6 |
|
After execution, an error message is displayed: TypeError: Argument 1 passed to dosth() must be callable
The program cannot execute the internal processing of dosth. The parameter type has been checked and processed for protection.
And if the callable is removed
1 2 3 4 5 6 |
|
After execution, a warning is displayed: Warning: call_user_func() expects parameter 1 to be a valid callback, function 'abc' not found or invalid function name
The program can execute the internal processing of dosth, so a lot of extra work needs to be done to check whether this callback method can be called.
Therefore, if the parameter of the method is a callback method, callable should be added to force it to be specified as the callback type. This can reduce calling errors and improve the quality of the program.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP array addition operation and the difference from array_merge
##php Detailed explanation of PDO exception handling method
PHP built-in encryption function
The above is the detailed content of PHP implements a method to force the callback type to be specified based on Callable. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
