PHP callback function and anonymous function use detailed

Source: Internet
Author: User
This time for you to bring PHP callback functions and anonymous functions in detail, the use of PHP callback functions and anonymous functions of the attention to what, the following is the actual case, together to see.

This paper describes the concept and usage of PHP callback function and anonymous function. Share to everyone for your reference, as follows:

1. Callback function

In fact, the callback function of PHP is exactly the same as the function of the callback function of C, Java and so on, all of which are in the process of executing the main thread and suddenly jump to execute the set callback function;

After the callback function is executed, go back to the main thread to process the next process

Instead of using the function name as a function parameter in PHP, like C and Java, the callback function is executed in PHP using the corresponding string name of the function.

1.1. No parameter callback

<?php//no parameter callback function callback () {  echo ' execute no Parameters callback.<br/> ';} function Main ($callback) {  echo ' execute main start.<br/> ';  $callback ();  Echo ' execute main end.<br/> ';} Main (' callback ');//result ecute main start.execute no parameters callback.execute main end.

1.2. Global callback function

<?php//Global function callback function callback ($a, $b) {  echo "$a <====> $b .<br/>";} $func = ' callback '; Call_user_func ($func), Call_user_func_array ($func, array);//Results 1<====>2.1<==== >2.

1.3. Class method and static method callback

<?phpclass test{  //member functions function  callback ($a, $b) {    echo "callback $a <====> $b .<br/>";  }  public static function Staticcallback ($a, $b) {    echo "Staticcallback $a <====> $b .<br/>";  }} The non-static method calls a $test = new test (), Call_user_func (Array ($test, ' callback '), and Call_user_func_array (Array ($test, ' Callback '), array, or//non-static method call mode Two $func = ' callback '; $test-$func (7,9);//static method call mode Call_user_func (' Test ' , ' Staticcallback '), 4,6), Call_user_func_array (Array (' Test ', ' staticcallback '), Array (4,6)); Call_user_func_array (" Test::staticcallback ", Array (4,6));//Results callback 1<====>2.callback 1<====>2.callback 7<====>9. Staticcallback 4<====>6.staticcallback 4<====>6.staticcallback 4<====>6.

2. anonymous function

2.1, the anonymous function in PHP (Anonymous functions), also known as the closure function (closures), allows you to specify a function without a name. The most common is the parameter value of the callback function

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

2.2. Closed Package

2.2.1, passing parameters, referencing local variables

<?php$closurefunc = function ($name) {  $sex = ' male ';  $func = function ($age) use ($name, $sex) {    echo "$name--$sex--$age <br/>";  };  $func (23);}; $func = $closureFunc ("LVFK");//Results lvfk--male--23

2.2.2, return closure function

<?php$closurefunc = function ($name) {  echo ' closurefunc ';  $sex = ' Male ';  echo "$name + + + $sex <br/>";  $func = function () use ($name, $sex) {    echo "$name--$sex <br/>";  };  return $func;}; $func = $closureFunc ("LVFK"); $func (); $func ();//Results Closurefunc lvfk+++ male lvfk--male lvfk--male

2.2.3, closures change the value of the context and require reference passing

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

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Direct implementation to generate poster ads

PHP to determine whether to open or open the browser

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.