How to get all methods of a class in php

王林
Release: 2023-03-04 13:24:01
Original
2813 people have browsed it

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 )
Copy after login

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
?>
Copy after login

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";
}
Copy after login

Output Result:

myclass
myfunc1
myfunc2
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template