Home  >  Article  >  Backend Development  >  How to query the methods in a class in php

How to query the methods in a class in php

青灯夜游
青灯夜游Original
2022-01-07 18:58:292200browse

In PHP, you can use the get_class_methods() function to query what methods are in a class. This function can get all the methods of the class and return an array composed of the method names of the class. The syntax is "get_class_methods(class name) ')".

How to query the methods in a class in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use the get_class_methods() function To query what methods are in the class.

The get_class_methods() function can get all the methods of the class and return an array composed of the method names of the class.

Syntax:

get_class_methods($class_name)
  • $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(&#39;myclass&#39;);
// or
$class_methods = get_class_methods(new myclass());

foreach ($class_methods as $method_name) {
    echo "$method_name<br>";
}

?>

Output result:

How to query the methods in a class in php

##Note:

Since PHP 5, this The function returns the method by its defined name (case-sensitive). In PHP 4 always returns lowercase.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to query the methods in 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