Home  >  Article  >  Backend Development  >  How to get the method name in php

How to get the method name in php

青灯夜游
青灯夜游Original
2022-02-10 12:51:263847browse

Getting method: 1. Use "__FUNCTION__" to get the name of the current method; 2. Use "__METHOD__" to get the current method name (including class name); 3. Use the get_class_methods() function, Gets all method names of the specified class.

How to get the method name in php

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

php Get method name

1. Use magic constants __FUNCTION__

__FUNCTION__: the name of the current function (or method);

<?php
header("Content-type:text/html;charset=utf-8");
class Website {
	public function demo() {
		echo &#39;成员方法名&#39;.__FUNCTION__;
	}
}
$student = new Website();
$student -> demo();
?>

How to get the method name in php

2. Use magic constants __METHOD__

##__METHOD__: the current method name (including class name); returns the value when the method is defined Name (case sensitive).

<?php
header("Content-type:text/html;charset=utf-8");
class Website {
	public function demo() {
		echo &#39;类名+方法名&#39;.__METHOD__;
	}
}
$student = new Website();
$student -> demo();
?>

How to get the method name in php

3. get_class_methods() function

get_class_methods - Get all the method names of the class and form an array

get_class_methods(mixed $class_name): array

Returns an array consisting of 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>";
}

?>

How to get the method name in php

Recommended learning: "

PHP Video Tutorial"

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