How to get class method in php

王林
Release: 2023-03-07 14:38:01
Original
2692 people have browsed it

The way PHP obtains class methods is to use the get_class_method function to obtain them, such as [get_class_methods(new myclass())]. This function returns an array of class method names.

How to get class method in php

#To get all the method names in a class, you can use the function get_class_method.

(Learning video recommendation: java video tutorial)

Function introduction:

get_class_methods function returns an array composed of the method names of the class.

Syntax:

get_class_methods ( mixed $class_name ) : array
Copy after login

Parameters:

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\n";
}

?>
Copy after login

Output:

myclass
myfunc1
myfunc2
Copy after login

Related recommendations: php training

The above is the detailed content of How to get class method 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