Home  >  Article  >  Backend Development  >  How to write php callback function

How to write php callback function

silencement
silencementOriginal
2019-09-24 14:00:553855browse

How to write php callback function

The definition and calling of ordinary functions are similar to js. This definition method does not require a return value, even if there is a return value, there is no need to add it when declaring.

In later PHP versions, a very useful feature was added, which is that it can be called before the function is defined.

echo add(1,2);
echo "</br>";
function add($a,$b){
    return $a+$b;
}
function sub($a,$b){
    return $a-$b;
}
echo add(23,12);
echo "</br>";
echo sub(23,22);
echo "</br>";

The following is a very useful function, which is the variable function. As the name suggests, the function is used as a variable.

The advantage is that different functions can be called with the same variable, which is very similar to polymorphic calling of functions.

$var="add";
echo $var(4,2);
echo "</br>";
$var="sub";
echo $var(4,2);
echo "</br>";

The callback function is to pass a process to a function when passing a simple parameter cannot solve the problem, so as to achieve the purpose.

Passing a function as a parameter when calling a function is a function callback.

$arr=array(2,3,5,4,1,6,7,9,8);
var_dump($arr);
echo "</br>";
//这里是自定义回调函数,返回-1是指将两个元素交换,0和1是不发生改变。
function myrule($a,$b){
    if ($a>$b){
        return 1;
    }
    elseif ($a<$b){
        return -1;
    }
    else{
        return 0;
    }
}
//usort就是系统函数,但是他的第二个参数是回调函数,这个函数参数就是排序规则
usort($arr,"myrule");
var_dump($arr);
echo "</br>";

Write your own callback function and use variable functions and callbacks to complete a simple filtering condition. If multiple conditions need to be satisfied at the same time, just give it an && relationship.

The variable function used can be called using a function called call_user_func_array() in the system. It has two parameters: the name of the callback function and the parameter array

call_user_func_array("demo ", array(1,3)); Its advantage is that the number of parameters in array can be less than that of the original function, even when there are default parameters.

//rule1除去数组中是三的倍数的数
function rule1($a){
    if ($a%3==0){
        return true;
    }else{
        return false;
    }
}
//rule2是除去数组中的回文数(从左边读与从右边读是一样的)
function rule2($a){
    if ($a==strrev($a)){
        return true;
    }else{
        return false;
    }
}
function demo($n,$var){
    for ($i=0;$i<$n;$i++){
        if (call_user_func_array($var,array(23)))
        //if ($var($i))
        {
            continue;
        }else{
            echo $i."<br>";
        }
    }
}
$var="rule1";
demo(100,$var);
echo "</br>";
echo "<hr>";
$var="rule2";
demo(200,$var);
echo "</br>";

Note that when calling a method in an object, we need to pass in an anonymous object and then specify the function name

. When calling a static method in a class, we only need to specify the class name.

In the above two cases, the variable function cannot be completely used. You can only use the system callback call_user_func_array(), which is just a variable function to pass parameters without calling

class A{
    function one(){
    }
    static function two(){
    }}demo(200,array(new A,"one"));demo(200,array("A","two"));

The above is the detailed content of How to write php callback function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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