How does a PHP function return a class method name?

PHPz
Release: 2024-04-10 16:45:01
Original
429 people have browsed it

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

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()
    {
        // ...
    }
}
Copy after login

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

Output:

Array
(
    [0] => getName
    [1] => getEmail
)
Copy after login

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!

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