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