PHP callback functions using methods and considerations, callback function _php Tutorial

Source: Internet
Author: User

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

  • 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.