PHP callback functions using methods and considerations, callback function
In PHP, some functions such as call_user_function () or Usort () accept user-defined functions as a parameter. The Callback function can be not only a simple function, it can also be a method of an object, including a static class method.
A PHP function is passed with a function name string. You can pass any built-in or user-defined functions, except for array (), Echo (), Empty (), eval (), exit (), Isset (), list (), print (), and unset ().
An object's method is passed as an array, the array's 0 subscript indicates the object name, and subscript 1 indicates the method name.
For static classes that are not instantiated as objects, to pass their methods, replace the object name indicated by the array 0 subscript with the name of the class.
Callback Function Example:
Copy the Code code as follows:
<?php
An example callback function
function My_callback_function () {
echo ' Hello world! ';
}
An example callback method
Class MyClass {
function Mycallbackmethod () {
Echo ' Hello world! ';
}
}
Type 1:simple Callback
Call_user_func (' my_callback_function ');
Type 2:static class method call
Call_user_func (Array (' MyClass ', ' Mycallbackmethod '));
Type 3:object method call
$obj = new MyClass ();
Call_user_func (Array ($obj, ' Mycallbackmethod '));
?>
http://www.phpe.net/manual/language.pseudo-types.php
http://cn.php.net/manual/en/language.pseudo-types.php
http://www.bkjia.com/PHPjc/946743.html www.bkjia.com true http://www.bkjia.com/PHPjc/946743.html techarticle PHP callback functions using methods and considerations, the callback function in PHP some functions such as call_user_function () or Usort () accept the user-defined function as a parameter. Ca ...