The concept and usage of PHP callback function and anonymous function

Source: Internet
Author: User

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

The concept and usage of PHP callback function and anonymous function

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.