Make code sweet. Some summary of non-general usage of PHP (2) ' callback ' callback in-php

Source: Internet
Author: User
Make code sweet. Some summary of PHP special usage (2) The ' callback ' callback in--php
This chapter summarizes some of the practical uses of ' callback ' in PHP.

1. What is callback?
First, a brief introduction of ' callback ', commonly known as ' Callback ', a behavior that passes a piece of executable code as a parameter for another function call, the callback has different implementations in different languages, almost all of the mainstream languages support this feature, and I summarize two main ways to implement callback:
1. Using function pointers to implement callbacks based on static languages such as C + +, C #, etc.
2. Implement callbacks through lambda expressions (also called anonymous functions) based on dynamic languages such as Ruby and JavaScript
A detailed definition of ' callback ' can be see Wikipedia: Http://en.wikipedia.org/wiki/Callback_ (computer_programming),

Complex callback in 2.PHP
Speaking of callback, many people immediately recall the implementation of the lambda function of PHP 5.3, this is indeed the most convenient way to achieve callback, if you have the opportunity to use PHP 5.3, be sure to use it mercilessly. However, for those who cannot use PHP 5.3 or must be compatible with the low version of the application, in fact, there are many other PHP callback implementation, the following one by one.
  • PHP supports function pointers in a way that implements the following rules:
  • If a function is passed, the function name is passed to the argument as a string
    If a method of an object is passed, an array is passed to the argument, where the position of the Index=0 object is placed, the index=1 position is placed in the method name string
    If a static method of a class is passed, an array is passed to the parameter, where the index=0 position is placed in the class name string, and the index=1 position is placed in the method name string

  • PHP supports using function create_function to create anonymous functions
  • This is the only way to create an anonymous function under PHP 5.3, with the following prototype:
    String Create_function (String $args, String $code)

    However, in this function, the function's argument list and implementation are placed in the string, in the editor can not be syntax highlighting, it is easy to error, but also to escape the characters in the code, which limits the use of anonymous functions in PHP.

  • that's what we mentioned earlier. Using Lambda functions (PHP 5.3 and later only)
  • With the popularity of closures and lambda functions in languages, PHP also supports lambda functions, which are modeled after the anonymous functions of JavaScript, but unlike the closure of Ruby and JavaScript, it does not have access to the enclosing variables. Unless using use to introduce variables to the display.

    In the PHP standard library, such as: Array_map,usort and other functions support the passing of callback parameters, you can use any of these methods as parameters. This is enough, to implement a function, the user can pass the callback parameter? Of course no problem, in the parameter list there is a callback parameter, write the implementation of the function, suddenly Dayton, and then asked the following questions:

    3. How to call callback in a function
    Yes, the above mentioned how to build the callback parameter, how to execute this parameter in the function? The easiest way to think about it is to execute a function directly using a string variableLike what:
    $a = ' functionname '; $a ();

    This method is intuitive and simple. But this method is not valid for all callback form parameters, If you want to be compatible with all callback parameters, you can use the following functions:

    Call_user_func and Call_user_func_array
    The purpose of these two functions is the same, the difference is that the Call_user_func_array function passed to the callback function parameters must be arranged in array, using which by personal preference, I tend to use call_user_func, fewer characters, more flexible parameters. The prototype of the function is as follows:
    Mixed Call_user_func (callback $function [, Mixed $parameter [, mixed $ ...]) Mixed Call_user_func_array (callback $function, array $param _arr)

    To improve the fault tolerance of the function, we it is necessary to determine whether a parameter can be called before it is called, which is a function that can be used as follows:

    is_callable
    The function prototypes are as follows:
    BOOL Is_callable (callback $name [, BOOL $syntax _only= false [, String & $callable _name]])


    The callback in PHP is summed up here. My Weibo address: Http://weibo.com/jameren, welcome to the attention of all.
    By the way, make an ad:
    Reference


    I recently used 15G ultra-large space free network USB ―@ cool disk, can be used on PC and mobile phone, no file type and size restrictions, transmission speed, file never lost, come together experience it! By registering with the invitation link below, you and I will have an extra 256MB of cool disk space! Http://t.cn/aNLZje

  • 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.