How to get the method name of the current class in php

王林
Release: 2023-03-11 13:36:01
Original
1866 people have browsed it

The way php gets the method name of the current class is to use the magic constant __METHOD__ to get it, for example [function test() {echo 'function name:' . __METHOD__;}].

How to get the method name of the current class in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

php provides us with __FILE__, __DIR__, __LINE__, __CLASS__, __NAMESPACE__, __METHOD__, __FUNCTION__ and other magic variables, among which:

__FILE__: Returns the full path and file name of the file.

__DIR__: Returns the directory of the file.

__LINE__: Returns the line number of the current file.

__CLASS__: Returns the class name.

__NAMESPACE__: Returns the name of the current namespace.

__METHOD__: Returns the method name of the class.

__FUNCTION__: Returns the current function name.

Let’s take a look at the __METHOD__ magic constant.

It represents the method name of the class (newly added in PHP 5.0.0). Returns the name of the method as it was defined (case-sensitive).

Example:

<?php
function test() {
    echo  &#39;函数名为:&#39; . __METHOD__ ;
}
test();
?>
Copy after login

Output result:

函数名为:test
Copy after login

Related video tutorial sharing: php video tutorial

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