Home  >  Article  >  Backend Development  >  How to view class methods in php

How to view class methods in php

王林
王林Original
2020-08-20 15:36:052528browse

To view the methods of classes in php, we can use the get_class_methods() method. This function can return an array consisting of class method names, for example: [get_class_methods($my_object)].

How to view class methods in php

get_class_methods — Returns an array consisting of class method names.

(Recommended tutorial: php graphic tutorial)

Description

array get_class_methods ( mixed $class_name )

Returns an array consisting of the method names defined in the class specified by class_name. If an error occurs, NULL is returned.

Note: Starting from PHP 4.0.6, you can specify the object itself instead of class_name, such as

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

(Learning video recommendation: php video tutorial)

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 view class methods 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