Home >Backend Development >PHP Tutorial >How to use PHP callback function and precautions, callback function_PHP tutorial

How to use PHP callback function and precautions, callback function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:20877browse

How to use PHP callback function and precautions, callback function

In PHP, some functions such as call_user_function() or usort() accept a user-defined function as a parameter. The Callback function can not only be a simple function, it can also be a method of an object, including methods of static classes.

A PHP function is passed as a function name string. You can pass any built-in or user-defined function, except array(), echo(), empty(), eval(), exit(), isset(), list(), print() and unset() .

The method of an object is passed in the form of an array. The 0 subscript of the array specifies the object name, and the subscript 1 specifies the method name.

For a static class that has not been instantiated as an object, to pass its method, replace the object name specified by the array 0 subscript with the name of the class.

Callback function example:

Copy code The code is as follows:

// 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/946743.htmlTechArticleHow to use the PHP callback function and precautions. There are some callback functions in PHP such as call_user_function() or usort() The function accepts a user-defined function as a parameter. Ca...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn