PHP callback function and anonymous function use case resolution

Source: Internet
Author: User
This time for you to bring the PHP callback function and anonymous function use case resolution, PHP callback function and anonymous function use of considerations, the following is the actual case, together to see.

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

The above is a simple application of the closure, through the closure, you can see the use of closures outside the function, the contents of the packet into the closure, can actually be the content of the context object,

You can also change the value of a context object within a closure, but it must be a reference pass

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 implementation of one-way hash encryption operation steps

PHP Hidden Portal File steps

Related Article

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.