This article mainly introduces how to force specify the callback type in PHPCallable, which is very good and has reference value. if you need a method, you can refer to the following if you need to accept a callback method as a parameter, we can write it like this
Output:
Do something callback
However, we cannot determine whether the callback method can be called. Therefore, we need to do a lot of extra work to check whether the callback method can be called.
Is there any better way to determine whether the callback method can be called?
We can use callable to forcibly specify the parameter as the callback type, which ensures that the callback method must be callable.
For example, the callback method is a non-existent method.
After the command is executed, the following error occurs: TypeError: Argument 1 passed to dosomething () must be callable.
The program cannot execute the processing in DoSomething, and check and process the parameter type to protect it.
If callable is removed
After the command is executed, a Warning is prompted: 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 processing inside DoSomething, so it requires a lot of extra work to check whether the callback method can be called.
Therefore, if the method parameter is a callback method, you must add callable to specify the callback type forcibly. This can reduce call errors and improve program quality.
The above is a small compilation of PHP Callable's method to force specify the callback type. I hope it will be helpful to you. if you have any questions, please leave a message and I will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!
For more articles about how PHP Callable forces to specify the callback type, refer to PHP!