Implementing Callbacks in PHP
The term "callback" in PHP encompasses both strings and arrays that operate as function pointers. In PHP 4, the following syntax emerged:
Although PHP 5.2.3 introduced callable syntax, strings containing such syntax cannot be directly invoked. Legacy syntax for PHP 4 includes:
The following code snippet demonstrates safe usage of callable values:
if (is_callable($cb2)) {
$returnValue = call_user_func($cb2, $arg1, $arg2);
}
Modern PHP versions support invoking the first three formats above directly as $cb(). Additionally, call_user_func and call_user_func_array support all the presented formats.
Notes and Caveats:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3