Home >Backend Development >PHP Problem >How to get the method name in php
Getting method: 1. Use "__FUNCTION__" to get the name of the current method; 2. Use "__METHOD__" to get the current method name (including class name); 3. Use the get_class_methods() function, Gets all method names of the specified class.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php Get method name
1. Use magic constants __FUNCTION__
__FUNCTION__: the name of the current function (or method);
<?php header("Content-type:text/html;charset=utf-8"); class Website { public function demo() { echo '成员方法名'.__FUNCTION__; } } $student = new Website(); $student -> demo(); ?>
2. Use magic constants __METHOD__
<?php header("Content-type:text/html;charset=utf-8"); class Website { public function demo() { echo '类名+方法名'.__METHOD__; } } $student = new Website(); $student -> demo(); ?>
3. get_class_methods() function
get_class_methods - Get all the method names of the class and form an arrayget_class_methods(mixed $class_name): arrayReturns an array consisting of method names defined in the class specified by class_name. If an error occurs, null is returned. Example:
<?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()); foreach ($class_methods as $method_name) { echo "$method_name <br>"; } ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to get the method name in php. For more information, please follow other related articles on the PHP Chinese website!