callback Function of PHP function (c) method callback of Class static function and Object

Source: Internet
Author: User
Tags php framework php programming
description of method callback for PHP class static functions and objects

Some of the previous chapters are declared and applied by global functions (which do not have functions defined in any object or class), but are different if static methods in the callback class are encountered, or ordinary methods in the object. Object-oriented technology will be described in detail in the chapters later in this book, so for this section of this application, you can go back to the learning and application of the need, in retrospect to consult the study. callback method, what if it is a static method of a class or a member method of an object? In the previous section we introduced the Call_user_func_array () function of the callback function of the PHP function, and we can review the application of the Call_user_func_array () function. You can change the first parameter function name string to an array-type parameter.

The following is a method of declaring a static member in the class by declaring a class separately. Use system functions to call static member methods and instance object member methods in a class.

Its code is as follows:

<?php//declares a class demo, declaring a static member method in a class fun () class demo{   static function Fun ($str 1, $str 2) {     echo "str1 =". $str 1;     echo "<br>";     echo "str2 =". $str 2;   }} Declares a class test, declaring a static member method in a class fun () class test{   function Fun ($str 1, $str 2) {     echo "str1 =". $str 1;     echo "<br>";     echo "str2 =". $str 2;   }} Using the system function Call_user_func_array (), call the static member method in the Demo class Fun () Call_user_func_array (Array ("Demo", "Fun"), Array ("php.cn", " "<br>------------------------<br>");//using the system function Call_user_func_array (), Call the Instance object member method in the test class. Call_user_func_array (Array (new test (), "Fun"), Array ("thinkphp", "PHP framework")?>

Description: All custom callback functions implemented using the Call_user_func_array () function, or all of the callback functions provided to us in the PHP system, can be used like this function to use the array type value in the first argument. And there must be two elements in the array: If you call a member method in a class, you need to specify the first element in the array parameter as the class name string, the second element as the static method name string in the class, or the first element in the array to be a reference to the object if the member method name in the object is called. The second element is the member name string in the object. The second parameter of the Call_user_func_array () function uses no change.

The description format of the callback function is summarized as follows, where callback () represents all the callback functions:

1. The format of the callback global function is as follows:

Callback ("function name string")

2. The format of the static member method in the callback class is as follows:

Callback (Array ("Class name string", "static method name string in class"))

3. The format of the member methods in the callback object is as follows:

Callback (Array (object reference, method name string in object))

The callback function provided to us by the system is identical to our custom callback function on the calling method. at this point, the callback function section of the PHP function is complete.


"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. PHP programming from getting started to mastering the full set of video tutorials

3. PHP real-Combat video tutorial

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.