There are two ways for php to get the name of the method in the class: 1. Use the magic constant "__FUNCTION__" to return the name of the current method in the class. 2. Use the get_class_methods() function to get the names of all methods in the specified class and return an array containing all method names. The syntax is "get_class_methods('class name')" or "get_class_methods(new class name())".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php is getting the class Two methods for method names
Method 1: Use magic constants__FUNCTION__
##__FUNCTION__ : Returns the name of the current method in the class.
<?php header('content-type:text/html;charset=utf-8'); class myclass { // constructor function myclass() { echo '成员方法名为:'.__FUNCTION__."<br>"; } // method 1 function myfunc1() { echo '成员方法名为:'.__FUNCTION__."<br>"; } // method 2 function myfunc2() { echo '成员方法名为:'.__FUNCTION__."<br>"; } } $class_methods = new myclass(); $class_methods -> myfunc1(); $class_methods -> myfunc2(); ?>
Method 2: Use the get_class_methods() function
get_class_methods - Get all the method names of the class and form an array ;If an error occurs, null is returned.<?php class myclass { // constructor function myclass() { return(true); } // method 1 function myfunc1() { return(true); } // method 2 function myfunc2() { return(true); } } $class_methods = get_class_methods('myclass'); // or $class_methods = get_class_methods(new myclass()); var_dump($class_methods); ?>
PHP Video Tutorial"
The above is the detailed content of How to get the method name in a class in php. For more information, please follow other related articles on the PHP Chinese website!