PHP callback function (callback)

Source: Internet
Author: User

The following content is from the official Php.net document

Callbacks

Functions such as Call_user_func (), Usort () accept user-defined callback functions as parameters.

A callback function can be more than just a simple function, or it can be an object method, including a static method of an object.

PHP functions can be called by the function name of a string type, and any built-in or user-defined functions can be used. But that doesn't include

PHP language structure, such as: Array (), Echo,empty (), eval (), exit (), Isset (), List (), Print,unset ()

The method of invoking a class is called by using the name of the instantiated object and the calling method, respectively, as the No. 0 and 1th parameters of the array.

A static method that invokes a class can invoke the name of the class and the name of the static method as the No. 0 and 1th arguments of the array, respectively, without instantiating the object of the class.

After php5.2.3, you can also invoke the static method of the class by using Classname::methodname.

In addition to these generic user-defined functions, anonymous functions can also be used as function arguments.


<?php//An example callback Functionfunction my_callback_function () {    echo ' Hello world! ';} An example callback Methodclass MyClass {    static function Mycallbackmethod () {        echo ' Hello world! ';    }} Type 1:simple callbackcall_user_func (' my_callback_function '); Type 2:static class Method Callcall_user_func (Array (' MyClass ', ' Mycallbackmethod ')); Type 3:object Method call$obj = new MyClass () Call_user_func (Array ($obj, ' Mycallbackmethod '));//Type 4:static class Method call (as of PHP 5.2.3) call_user_func (' Myclass::mycallbackmethod ');//Type 5:relative Static class method call (as of PHP 5.3.0) class A {public    static function who () {        echo "a\n";    }} Class B extends A {public    static function who () {        echo "b\n";    


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.