Home  >  Article  >  Backend Development  >  How to get all methods of a class in php

How to get all methods of a class in php

王林
王林Original
2020-08-27 13:55:322233browse

php can use the get_class_methods() function to obtain all methods of a class. This function can return an array consisting of the method names of the class, or NULL if an error occurs. Function syntax: [get_class_methods($class_name)].

How to get all methods of a class in php

php provides us with a function get_class_methods, which can return an array composed of class method names, and returns NULL if an error occurs.

(Recommended video tutorial: php video tutorial)

Grammar:

array get_class_methods ( mixed $class_name )

(Related recommendation: php training)

Code 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(&#39;myclass&#39;);
 
// or
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name)
{
    echo "$method_name";
}

Output:

myclass
myfunc1
myfunc2

The above is the detailed content of How to get all methods of a class in php. 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