Home  >  Article  >  Backend Development  >  PHP function callback function (3) Method callbacks of static functions and objects

PHP function callback function (3) Method callbacks of static functions and objects

伊谢尔伦
伊谢尔伦Original
2017-05-13 11:27:203233browse

PHP class static function and object method callback description

In the previous chapters, global functions (not defined in any object or class function) declares and applies the callback function, but it will be different if it encounters a static method in the callback class, or an ordinary method in the object. Object-oriented technology will be introduced in detail in the later chapters of this book, so for the application method introduced in this section, you can go back and review it when necessary in subsequent studies and applications. What should we do if the callback method is a static method of a class or a member method in an object? In the previous chapter, we introduced the call_user_func_array() function of the callback function of the php function. We can review the application of the call_user_func_array() function again. You can change the first parameter, the function name string, to an array type parameter.

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

The code is as follows:

<?php
//声明一个类demo,类中声明一个静态成员方法fun()
 class demo{
   static function fun($str1,$str2){
     echo "str1 = ".$str1;
     echo "<br>";
     echo "str2 = ".$str2;
   }
 }
//声明一个类test,类中声明一个静态成员方法fun()
 class test{
   function fun($str1,$str2){
     echo "str1 = ".$str1;
     echo "<br>";
     echo "str2 = ".$str2;
   }
 }
//使用系统函数call_user_func_array(),调用demo类中的静态成员方法fun()
call_user_func_array( array("demo","fun"), array("php.cn", "php中文网"));
echo "<br>------------------------<br>";
//使用系统函数call_user_func_array(),调用test类中的实例对象成员方法fun()
call_user_func_array( array(new test(),"fun"), array("thinkphp","php框架"))
?>

Description: All custom callback functions implemented using the call_user_func_array() function, or for us in the PHP system All callback functions provided can use array type values ​​in the first parameter like this function, and two elements must be used in the array: if you call a member method in a class, you need to specify the first parameter in the array parameter. One element is a class name string, and the second element is a static method name string in the class; if the member method name in the object is called, the first element in this array is a reference to the object, and the second element is a reference to the object. The elements are strings of member names in the object. There is no change in the use of the second parameter of the call_user_func_array() function.

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

1. Format of the callback global function As shown below:

callback("函数名称字符串")

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

callback(array("类名称字符串","类中静态方法名称字符串"))

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

callback(array(对象引用,"对象中方法名称字符串"))

The callback function provided by the system and our customized callback function are called The methods are exactly the same. So far, The callback function part of the php function has been introduced.


【Related tutorial recommendations】

1. "php.cn Dugu Jiujian (4) -php video tutorial

2. A full set of video tutorials on php programming from entry to mastery

3. php practical video tutorial

The above is the detailed content of PHP function callback function (3) Method callbacks of static functions and objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn