Home  >  Article  >  Backend Development  >  How to understand php callbacks

How to understand php callbacks

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-29 10:35:532634browse

How to understand php callbacks

Callback function callback

We all know that when asynchronous requests are often processed in js, if you use ordinary function, may not be guaranteed to be called after the asynchronous request is completed. So there is a callback function, especially during file processing and ajax processing, the callback function is very useful.

Function call_user_func()

mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $… ]] )

return Returns a The value of the composite type is actually the value returned by the callback function

callable $callback The callback function can be a function name, an array, or a string

mixed $parameter The parameter of the function, Display in a sequential manner

Let’s introduce the use of demo

demo1: callback php function string form

"callback" , "value"=>"test");
$rs1 = http_build_query($data);      //直接调用php函数
$rs2  = call_user_func("http_build_query",$data); //使用回调函数
echo $rs1;  //name=callback&value=test
echo "
"; echo $rs2; //name=callback&value=test // 这里需要注意的是,参数1必须是可使用的函数可以通过function_exists()返回true的函数,这里提醒isset,empty,is_null 等认为的常用函数实际上是一个操作符.并不能算函数。

Related recommendations: "PHP Getting Started Tutorial

demo2: Callback php function function name variable format

"callback" , "value"=>"天才");
$str  = http_build_query($data); 
$rs1  = urldecode($str);      //urlencode编码
$rs2  = call_user_func(myUrldecode,$str);
echo $rs1;  //name=callback&value=天才
echo "
"; echo $rs2; //name=callback&value=天才 //这里我们可以看到,我们直接使用函数的名称也是可以的,不需要带引号字符串。 ?>

demo3: Callback class method array format

"; //?query=/test/demo1
echo $decode;
//注意 使用方法名也具有作用域的概念,即private protected 和 public,通常回调类方法都只能调用publi 和默认作用域的 方法。
//同时如果是普通方法,并且内部使用了$this变量,那么进行调用是无法成功的.
?>

demo4: Callback class method string format

";
  echo $decode; //  ?query=/test/demo1
  $encode2  = call_user_func("MyClass::myUrlencode2",$str);
  var_dump($encode2);  // null
//如果直接使用字符串的方法的话,那么必须在类和方法名中添加::作为分割。
//这里我们发现不是静态方法也可以用::进行调用
//这里进行了一个测试,发现调用private 作用域的方法返回的是一个null值,说明确实存在作用域的关系
?>

demo5: Callback object method array format

demo ; //调用内部的this作用域
    }
    static  function myUrlencode($str){
        return urlencode($str);
    }
}
 $str = "?query=/test/demo1";
 $class =  new MyClass();
 $encode  = call_user_func(array($class,"myUrlencode"),$str);
 $decode  = call_user_func(array($class,"myUrldecode"),$str);
 echo $encode; //%3Fquery%3D%2Ftest%2Fdemo1
 echo "
"; echo $decode; //?query=/test/demo1-test //很明显,如果使用对象做为回调函数,内部的private 属性和方法也可以使用,但是对外的方法必须为默认或者 public类型 //对象数组方式第一个选项必须为一个对象 ?>

Sometimes we It is found that the parameters passed may not be fixed, so using call_user_func() will cause some trouble. At this time, you can use the call_user_func_array() method. Most of the usage methods are the same, but the use of parameters will be a little different. different.

Function call_user_func_array()

demo1: callback php function indefinite parameters

 2135,
    );
    $record2 = array(
        'first_name' => 'Sally',
    );
    $record3 = array(
        'last_name' => 'Jones',
    );
$return  = array_merge($record1,$record2,$record3);
//如果使用回调函数,则如下
$return  = call_user_func_array("array_merge", array($record1,$record2,$record3));
print_r($return); //Array ( [id] => 2135 [first_name] => Sally [last_name] => Jones )
//一些特别的函数允许你有不固定的参数,如果你还将它作为回调函数,则可以使用这个方法进行调用.
?>

tips1 How to determine whether it is Callable function

bool is_callable ( callable $name [, bool $syntax_only = false [, string &$callable_name ]] )

name

to be checked Callback.

syntax_only

If set to TRUE, this function only verifies that name may be a function or method.

callable_name

Accepts "callable name".

return

Returns TRUE if name is callable, otherwise returns FALSE.

tips2 Get the parameters of the function, similar to arguments in js

$val){
 $sum += (int) $val; 
}
return $sum;
}
$rs = sum(1,2,3,4,5);
var_dump($rs); //  int 15
//注意: 在sum函数中修改了$first,$second的变量,同时又不是引用对象 变量的话,则不会修改
?>

The above is the detailed content of How to understand php callbacks. 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