Analysis of Concept and Usage of PHP Callback Function

  • 2021-08-16 23:16:28
  • OfStack

In this paper, the concept and usage of PHP callback function are described with examples. Share it for your reference, as follows:

1. The concept of callback function

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

The concept of callback function in other languages is similar, However, the implementation mechanism of callback function in various languages is different. Generally speaking, callback function is a function defined by us, but it is not called directly by us, but by another function. This function calls it by receiving the name and parameters of callback function.

2. Implementation of 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 as an array, as can be seen from its prototype: mixed call_user_func_array (callable $callback, array $param_arr), which has only two parameters. The number of parameters of call_user_func ($callback, parameter 1, parameter 2,...) is determined according to the parameters of the callback function.

How to realize the callback to the global function in the script, the non-static method that does not use $this in the class, the non-static method that uses $this in the class (need to pass in the object), and the static method in the class? The following is the code that passed the test.


<?php
// Ordinary function 
function f1($arg1,$arg2)
{
  echo __FUNCTION__.'exec,the args is:'.$arg1.' '.$arg2;
  echo "<br/>";
}
// Pass call_user_func Call function f1
call_user_func('f1','han','wen');
  // Pass call_user_func_array Call 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/>";
  }
}
// Calls a non-static member function of the class, which has a $this Called a member in an object 
$a = new A;
$a->name = 'wen';
call_user_func_array(array($a,'show',),array('han!'));
// Call a non-static member function of the class, no object is created, and the member function cannot have $this
call_user_func_array(array('A','show1',),array('han!','wen'));
// Calling static member functions in a class 
call_user_func_array(array('A','show2'),array('argument1','argument2'));

Run results:


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

For more readers interested in PHP related contents, please check the topics of this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Complete Collection of Operation Skills of PHP Array (Array)", "Tutorial on Data Structure and Algorithms of PHP" and "Summary of Programming Algorithms of php"

I hope this article is helpful to everyone's PHP programming.


Related articles: