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-14 15:17:352828browse

php method to obtain all methods of a class: You can use the get_class_methods() function to obtain it. The get_class_methods() function returns an array consisting of the method names of the class, or NULL if it fails.

How to get all methods of a class in php

get_class_methods() function returns an array consisting of the method names of the class.

(Recommended tutorial: php graphic tutorial)

Syntax:

array get_class_methods ( mixed $class_name )

Returns the method name defined in the class specified by class_name Array composed of. If an error occurs, NULL is returned.

Note: Starting with PHP 4.0.6, you can specify the object itself instead of class_name.

(Video tutorial recommendation: php video tutorial)

For example:

<?php
$class_methods = get_class_methods($my_object); // see below the full example
?>

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 Result:

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