How to get the method name in a class in php

青灯夜游
Release: 2023-03-16 11:26:01
Original
2408 people have browsed it

There are two ways for php to get the name of the method in the class: 1. Use the magic constant "__FUNCTION__" to return the name of the current method in the class. 2. Use the get_class_methods() function to get the names of all methods in the specified class and return an array containing all method names. The syntax is "get_class_methods('class name')" or "get_class_methods(new class name())".

How to get the method name in a class in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

php is getting the class Two methods for method names

Method 1: Use magic constants__FUNCTION__

##__FUNCTION__ : Returns the name of the current method in the class.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
class myclass {
    // constructor
    function myclass()
    {
        echo &#39;成员方法名为:&#39;.__FUNCTION__."<br>";
    }

    // method 1
    function myfunc1()
    {
        echo &#39;成员方法名为:&#39;.__FUNCTION__."<br>";
    }

    // method 2
    function myfunc2()
    {
        echo &#39;成员方法名为:&#39;.__FUNCTION__."<br>";
    }
}

$class_methods = new myclass();
$class_methods -> myfunc1();
$class_methods -> myfunc2();
?>
Copy after login

How to get the method name in a class in php

Method 2: Use the get_class_methods() function

get_class_methods - Get all the method names of the class and form an array ;If an error occurs, null is returned.

<?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());

var_dump($class_methods);

?>
Copy after login

How to get the method name in a class in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to get the method name in a class in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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