Usage and precautions of the PHPcallback function, callback function _ PHP Tutorial

Source: Internet
Author: User
Call back functions. Methods and precautions for using the PHPcallback function. Some callback functions in PHP, such as call_user_function () or usort (), use user-defined functions as a parameter. Ca PHP callback function usage and precautions, callback function

Some functions in PHP, such as call_user_function () or usort (), use user-defined functions as a parameter. The Callback function can be not only a simple function, but also an object method, including a static class method.

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

The method of an object is transmitted as an array. the 0 subscript of the array indicates the object name, and the subscript 1 indicates the method name.

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

Callback function instance:

The code is 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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/946743.htmlTechArticlePHP callback functions use methods and notes, callback functions in PHP some functions such as call_user_function () or usort () accept user-defined functions as a parameter. Ca...

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.