How to get the method name in php

青灯夜游
Release: 2023-03-15 08:28:02
Original
3855 people have browsed it

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

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

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

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

?>
Copy after login

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!

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