Home >Backend Development >PHP Problem >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
<?php $data = array("name"=>"callback" , "value"=>"test"); $rs1 = http_build_query($data); //直接调用php函数 $rs2 = call_user_func("http_build_query",$data); //使用回调函数 echo $rs1; //name=callback&value=test echo "<br />"; 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
<?php //自定义函数 function myUrldecode($str){ return urldecode($str); } $data = array("name"=>"callback" , "value"=>"天才"); $str = http_build_query($data); $rs1 = urldecode($str); //urlencode编码 $rs2 = call_user_func(myUrldecode,$str); echo $rs1; //name=callback&value=天才 echo "<br />"; echo $rs2; //name=callback&value=天才 //这里我们可以看到,我们直接使用函数的名称也是可以的,不需要带引号字符串。 ?>
demo3: Callback class method array format
<?php class MyClass{ private $demo = "test"; function myUrldecode($str){ return urldecode($str); } static function myUrlencode($str){ return urlencode($str) ; } } $str = "?query=/test/demo1"; $encode = call_user_func(array(MyClass,"myUrlencode"),$str); //直接使用类的静态方法 将字符串进行url编码 不再是字符串或者函数名,而是一个数组格式,第一个项表示类名,第二个项则表示方 法名。 第一项可以为类的引用地址,第二项为静态方法名称 $decode = call_user_func(array("MyClass","myUrlencode"),$encode); //同样是使用类的方法,不过调用的是普通方法名称。 echo $encode; //%3Fquery%3D%2Ftest%2Fdemo1 echo "<br />"; //?query=/test/demo1 echo $decode; //注意 使用方法名也具有作用域的概念,即private protected 和 public,通常回调类方法都只能调用publi 和默认作用域的 方法。 //同时如果是普通方法,并且内部使用了$this变量,那么进行调用是无法成功的. ?>
demo4: Callback class method string format
<?php class MyClass{ private $demo = "test"; function myUrldecode($str){ return urldecode($str); } private function myUrldecode2($str){ return urldecode($str); } static function myUrlencode($str){ return urlencode($str) ; } } $str = "?query=/test/demo1"; $encode = call_user_func("MyClass::myUrlencode",$str); $decode = call_user_func("MyClass::myUrldecode",$encode); echo $encode; // %3Fquery%3D%2Ftest%2Fdemo1 echo "<br />"; echo $decode; // ?query=/test/demo1 $encode2 = call_user_func("MyClass::myUrlencode2",$str); var_dump($encode2); // null //如果直接使用字符串的方法的话,那么必须在类和方法名中添加::作为分割。 //这里我们发现不是静态方法也可以用::进行调用 //这里进行了一个测试,发现调用private 作用域的方法返回的是一个null值,说明确实存在作用域的关系 ?>
demo5: Callback object method array format
<?php class MyClass{ private $demo = "test"; function myUrldecode($str){ return urldecode($str) ."-" .$this->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 "<br />"; 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
<?php $record1 = array( 'id' => 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.
<?php // is_callable(); $bool = is_callable(urlencode); // true $bool = is_callable(urlencode,false); // true $bool = is_callable(urlencode,true); // true $bool = is_callable("urlencode",false,$callable_name1); // string urlencode $bool = is_callable("urldecode",true,$callable_name2); // string urldecode 即使不存在回调函数也会返回true ?>
tips2 Get the parameters of the function, similar to arguments in js
<?php //这里指的是非客户端php脚本 function sum($first,$second){ $args = func_get_args(); //获取列表 //func_get_arg($index) 则获取指定的位置的参数 $len = func_num_args(); //获取长度 $sum = 0 ; foreach($args as $key=>$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!