Home >Backend Development >PHP Tutorial >How does a PHP function return a class method name?

How does a PHP function return a class method name?

PHPz
PHPzOriginal
2024-04-10 16:45:01454browse

The get_class_methods() function in PHP can return an array of method names of the class. The parameter is an object, and the return value is a string array containing the class method name. It returns the public method name, including the method name of the parent class. If the argument is not an object, an empty array is returned.

PHP 函数如何返回类方法名?

#How does a PHP function return the class method name?

The get_class_methods() function in PHP can return an array of method names of a class.

Syntax

get_class_methods(object $object): array

Parameters

  • $object: The object to get the method name.

Return value

A string array containing the name of the class method.

Practical case

Consider the following class:

class User
{
    public function getName()
    {
        // ...
    }

    public function getEmail()
    {
        // ...
    }
}

To get the method name of the User class, you can use get_class_methods() Function:

$user = new User();
$methods = get_class_methods($user);

print_r($methods);

Output:

Array
(
    [0] => getName
    [1] => getEmail
)

Note

  • ##get_class_methods() The function returns the public method name.
  • If the object inherits from the parent class, the returned array will contain the method name of the parent class.
  • If the parameter passed is not an object, an empty array will be returned.

The above is the detailed content of How does a PHP function return a class method name?. 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