Analyze the concepts and usage of PHP callback functions, and analyze callback function instances

Source: Internet
Author: User

Analyze the concepts and usage of PHP callback functions, and analyze callback function instances

This article describes the concepts and usage of PHP callback functions. We will share this with you for your reference. The details are as follows:

I. Concepts of callback Functions

Let's take a look at the callback function in C: The callback function is a function called through the function pointer. If you pass the pointer (address) of a function as a parameter to another function, when the pointer is used to call the function to which it points, we will say this is a callback function. The callback function is called by another party when a specific event or condition occurs instead of by the implementer of the function. It is used to respond to the event or condition.

The concepts of callback functions in other languages are similar, except that the implementation mechanisms of callback functions in different languages are different. In general, callback functions are defined by us, but instead of calling it directly, we call it through another function. This function receives the name and parameters of the callback function to call it.

II. Implementation of callback functions in php

Php provides two built-in functionscall_user_func()Andcall_user_func_array()Provides support for callback functions. The difference between the two functions is that call_user_func_array receives callback function parameters in the form of arrays. You can see its prototype: mixed call_user_func_array (callable $ callback, array $ param_arr ), it has only two parameters. While call_user_func ($ callback, parameter 1, parameter 2 ,...) The number of parameters is determined based on the callback function parameters.

How to implement non-static methods that do not use $ this in the global functions in the script, non-static methods that use $ this in the class (the object needs to be passed in) and the callback of static methods in the class. below is the code that passes the test.

<? Php // normal function FUNCTION f1 ($ arg1, $ arg2) {echo _ function __. 'exec, the args is :'. $ arg1 .''. $ arg2; echo "<br/>" ;}// call the function f1call_user_func ('f1 ', 'Han', 'wen') through call_user_func '); // call the call_user_func_array function call_user_func_array ('f1 ', array ('han', 'wen'); class A {public $ name; function show ($ arg1) {echo 'the arg is :'. $ arg1. "<br/>"; echo 'My name is :'. $ this-> name; echo "<br/>";} function show1 ($ arg1, $ arg2) {echo __ METHOD __. 'exec, the args is :'. $ arg1 .''. $ arg2. "<br/>";} public static function show2 ($ arg1, $ arg2) {echo _ METHOD __. 'of class A exec, the args is :'. $ arg1 .''. $ arg2. "<br/>" ;}// call the non-static member function of the class. $ this in the member function calls the member $ a = new A in the object; $ a-> name = 'wen'; call_user_func_array (array ($ a, 'show ',), array ('Han! '); // Call the non-static member function of the class. No object is created. The member function cannot contain $ thiscall_user_func_array (array ('A', 'show1 ',), array ('Han! ', 'Wen'); // call the static member function call_user_func_array (array ('A', 'show2'), array ('argument1 ', 'argument2 '));

Running result:

f1exec,the args is:han wenf1exec,the args is:han wenthe arg is:han!my name is:wenA::show1 exec,the args is:han! wenA::show2 of class A exec, the args is:argument1 argument2

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.