is an example of the PHP callback function and the concept and usage of anonymous function. Share to you for your reference, as follows: the effect of


PHP in the callback function and callback function C, Java language is in the process are as like as two peas, the main thread of execution, suddenly jump to execute the callback function set;

the callback function is executed, and then back to the main thread to handle the next process in the PHP

calls the callback function, C and Java do not want to directly use a function name as a function parameter, but the use of the corresponding function execution string name in PHP


 < callback parameter; PHP / / no function callback parameter? Callback ('execute no) {echo parameters callback.< br/> ';} function {echo main ($callback)'execute main start.& L T; br/> $callback (echo) ';'execute main; end.< br/>';} main ('callback'); / / ecute main start. execute no parameters the callback. execute main end. 


 <, a global callback function; PHP? Function callback / / global callback function ($a, $b) {echo "$a< ====> $b.< br/> $func ='callback';}; call_user_func ($func, 1,2); call_user_func_array ($func, array (1,2)); / / 1< ====> 1< 2.; ====> 2.; 


 method and static method callback 

< PHP class Test{/ / member function? Function callback ($a, $b) {echo callback $a< ====> $b.< br/> public;} static function staticCallback" ($a, $b) {echo staticCallback $a< ====> $b.< br/>};} / / non static method Call a $test = new (Test); call_user_func (array ($test,'callback'), 1,2 (call_user_func_array); array ($test,'callback'), array (1,2)); / / non static method call two $func ='callback'; $test-> $func (7,9); / / call_user_func static method invocation (array ('Test','staticCallback', 4,6); call_user_func_array (array) ('Test','staticCallback'), array (4,6)); call_user_func_array ("Test:: staticCallback", array (4,6)); / / the callback 1< ====> callback 2. 1< ====> 2. callback; 7< ====> staticCallback 9. 4< ====> 6.; staticCallback 4< ====> ====> staticCallback 6. 4< 6.


2.1, an anonymous function in PHP (Anonymous functions), Also called the closure function (closures), allowing a function to be specified without a name. The most common parameter is a callback function value

 < PHP? $closureFunc = function ($str) {echo $str.'< br/> ';}; $closureFunc ("Hello world!"); / / Hello world 

2.2, the closure of


2.2.1, incoming parameters,

 < refers to a local variable; PHP? $closureFunc = function ($name) {$sex =' male '; $func = function ($age) use ($name, $sex) {echo $name--$sex--$age< br/> $func;}; (23);}; $func = $closureFunc ("lvfk"); / / lvfk-- --23 

2.2.2, the man returned to the closure function of

 < PHP? $closureFunc = function ($name) {echo $sex =''closureFunc ';' echo 'male; $name+++$sex< br/> $func; function = (use) ($name, $sex) {echo $name--$sex< br/> return; $func;} ";}; $func = $ ClosureFunc ("lvfk"); ($func); $func (closureFunc); / / the lvfk+++ male lvfk-- male lvfk-- male 

2.2.3, change the values of the context closure,

 transfer < reference;? PHP $closureFunc = function ($name) {$age = 1; $name+++$age< echo br/>; $func = function (use) ($name, & $age) {$age++; $name--$age< br/> echo; return $func;};}; $func = $closureFunc ("lvfk"); ($func); $func ($func); (); / / lvfk+++1 lvfk--2 lvfk--3 lvfk--4 

the above results

is a simple application of closure, the closure can be seen in the use of closure function outside, the incoming parameters content closure, can actually be context object content,

can also change the context object value in the closure, but Must be a reference

about the closure effect can reference:

PHP more about the related content of interested readers can see the special station: "php commonly used functions and skills summary ", "php string (string) usage summary" and "PHP array (Array) technique," PHP data structure and algorithm tutorial " and "" PHP programming algorithm summary the

hope for all PHP programming help.

you might be interested in this article:

This concludes the body part

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:PHP callback function and anonymous function concept and usage detailed solution | Script Home

You may also be interested in these articles!