The way PHP obtains class methods is to use the get_class_method function to obtain them, such as [get_class_methods(new myclass())]. This function returns an array of class method names.
#To get all the method names in a class, you can use the function get_class_method.
(Learning video recommendation: java video tutorial)
Function introduction:
get_class_methods function returns an array composed of the method names of the class.
Syntax:
get_class_methods ( mixed $class_name ) : array
Parameters:
class_name class name or object instance.
Return value:
Returns an array consisting of the 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\n"; } ?>
Output:
myclass myfunc1 myfunc2
Related recommendations: php training
The above is the detailed content of How to get class method in php. For more information, please follow other related articles on the PHP Chinese website!