PHP callback function using the detailed

Source: Internet
Author: User
This time to bring you a PHP callback function using the details of the PHP callback function to use the attention of what, the following is the actual case, take a look.

This paper describes the concept and usage of PHP callback function. Share to everyone for your reference, as follows:

First, the concept of callback function

First look at the callback function in C: The callback function is a function called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is a callback function. The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.

The concept of callback functions in other languages is similar, except that the implementation mechanism of callbacks in various languages is different, and in layman's terms, the callback function is a function we define, but it is not called directly, but by another function. This function implements a call to a callback function by receiving its name and arguments.

Second, the implementation of the callback function in PHP

PHP provides two built-in functions call_user_func() and call_user_func_array() provides support for callback functions. The difference between the two functions is that Call_user_func_array receives the parameters of the callback function in the form of an array, and the prototype will know:mixed Call_user_func_array (callable $ Callback, Array$param_arr), it has only two parameters. and Call_user_func ($callback, parameter 1, parameter 2, ...) The number of parameters is determined based on the parameters of the callback function.

How to implement callbacks for global functions in scripts, non-static methods that do not use $this in classes, non-static methods that use $this in classes (need to pass in objects), static methods in classes, and the code passed by the test.

<?php//normal functions function F1 ($arg 1, $arg 2) {echo function. ' Exec,the args is: '. $ Arg1. '  '. $arg 2; echo "<br/>";}  Call the function through Call_user_func f1call_user_func (' F1 ', ' Han ', ' Wen ');  The function Call_user_func_array (' F1 ', Array (' Han ', ' Wen ')) is called through Call_user_func_array, and class a{public $name; Function Show ($arg 1) {echo ' The arg is: '. $arg 1. "    <br/> ";    Echo ' My name is: '. $this->name;  echo "<br/>"; } function Show1 ($arg 1, $arg 2) {echo METHOD. ' Exec,the args is: '. $arg 1. ' '. $arg 2. '  <br/> "; The public static function Show2 ($arg 1, $arg 2) {echo METHOD. ' of class A exec, the args is: '. $arg 1. ' '. $arg 2. '  <br/> "; }}//calls a non-static member function in the class that has $this called the member in the object $ A = new A; $a->name = ' wen '; Call_user_func_array (Array ($a, ' show ',), Array (' han! ')); /Call class non-static member function, no object is created, this member function cannot have $thiscall_user_func_array (array (' A ', ' Show1 ',), Array (' han! ', ' Wen '));// Call the static member function in the Class Call_user_func_array (Array (' A ', ' show2 '), Array (' argument1 ', ' argument2 ')); 

Operation 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

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:

What should I do with the file_put_contents function in PHP?

How PHP operates Phar files

How PHP solves the longest common substring

What is the method for generating random numbers in PHP

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.