Makecodesweet. PHP non-general usage summary (2)-'callback' callback in PHP

Source: Internet
Author: User
Makecodesweet. PHP special usage summary (2) -- 'callback' callback in PHP this chapter summarizes some practical usage of 'callback' in PHP. 1. what is callback? & Nbsp; first, let's briefly introduce 'callback', commonly known as 'callback' and Make code sweet. summary of some special PHP Usage (2) -- 'callback' callback in PHP
This chapter summarizes some practical usage of 'callback' in PHP.

1. what is callback?
First, let's briefly introduce 'callback', which is commonly known as 'callback'. it is an action that passes an executable code as a parameter for another function to call, callback has different implementation methods in different languages. almost all mainstream languages now support this feature. in my summary, there are two main methods to implement callback:
1. callback is implemented using function pointers based on static languages such as C ++ and C #.
2. callback is implemented using lambda expressions (also called anonymous functions) based on Ruby, JavaScript, and other dynamic languages.
For detailed definitions of 'callback', see Wikipedia: http://en.wikipedia.org/wiki/Callback_ (computer_programming ),

2. complicated callback in PHP
Speaking of callback, many people think of the lambda function implementation method of PHP 5.3. this is indeed the most convenient and eye-catching method for implementing callback. if you have the opportunity to use PHP 5.3, be sure to use it. However, for applications that cannot use PHP 5.3 or must be compatible with earlier versions, there are still many other callback implementation methods in PHP. here we will introduce them one by one.
  • PHP supports function pointer implementationThe implementation rules are as follows:
  • If you pass a function, the function name is passed as a string to the parameter.
    If you pass an object method, an array is passed to the parameter, where index = 0 places the object, and index = 1 places the method name string.
    If you pass a static method of the class, an array is passed to the parameter, where index = 0 is placed in the class name string, and index = 1 is placed in the method name string.

  • PHP supports using the create_function function to create anonymous functions.
  • This is the only way to create anonymous functions in PHP versions earlier than 5.3. the prototype is as follows:
    string create_function ( string $args , string $code )

    However, this function adds the parameter list and implementation of the function to the string. Syntax highlighting is not allowed in the editor, which is prone to errors and escape characters in the code, these complexities limit the use of anonymous functions in PHP.

  • That is, the lambda function(Only supported by PHP 5.3 and later)
  • With the popularity of closures and lambda functions in languages, PHP also supports lambda functions. The syntax is similar to the anonymous functions of javascript, however, unlike the closure of Ruby and JavaScript, it cannot access the variable outside the closure, unless it uses use to display the variables.

    In the PHP Standard Library, functions such as array_map and usort support passing callback parameters. you can use any of the preceding methods as parameters. This is enough. if you want to implement a function, can you pass the callback parameter? Of course there is no problem. there is a callback parameter in the parameter list that says the implementation of the function, and suddenly a pause, and then asked the following question:

    3. how to call callback in a function
    Yes, I mentioned how to construct the callback parameter. how can I execute this parameter in the function? The easiest way to think of it is Directly use string variables to execute functionsFor example:
    $a='FunctionName';$a();

    This method is intuitive and simple. However, this method is not valid for all callback parameters, To be compatible with all callback parameters, you can use the following functions:

    Call_user_func and call_user_func_array
    The two functions have the same purpose. The difference is that the parameters passed by the call_user_func_array function to the callback function must be arranged in array. which function is used depends on your preferences? I prefer to use call_user_func with fewer characters, more flexible parameters. The function prototype is as follows:
    mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )mixed call_user_func_array ( callback $function , array $param_arr )

    To improve function fault tolerance, we You need to determine whether the parameter can be called before calling. the following functions can be used:

    Is_callable
    The function prototype is as follows:
    bool is_callable ( callback $name [, bool $syntax_only= false [, string &$callable_name ]] )


    The callback in PHP is summarized here. My Weibo address: http://weibo.com/jameren,welcome to your attention ,.
    By the way, make an advertisement:
    Reference
    I recently used a 15 GB ultra-high-space free network USB flash drive-@ cool disk, which can be used on PCs and mobile phones. it has no file type and size restrictions, fast transmission speed, and files will never be lost, let's try it together! Register through the invitation link below, and you and I will get an additional MB of cool disk space! Http://t.cn/aNLZje

    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.