Home > Backend Development > PHP Tutorial > How to get the current function name in PHP? (code example)

How to get the current function name in PHP? (code example)

青灯夜游
Release: 2023-04-05 14:58:02
Original
6212 people have browsed it

In PHP, we can use the magic constant __FUNCTION__ or __METHOD__ to easily obtain the function name. The following article will give you a detailed understanding. I hope it will be helpful to everyone.

How to get the current function name in PHP? (code example)

Method 1: Use __FUNCTION__

Magic constant __FUNCTION__ is used for parsing Function name or method name (function in class), function name can be returned.

Example:

<?php 
class Test { 
  
    public function bar() { 
        var_dump(__FUNCTION__); 
    } 
} 
  
function foo() { 
    var_dump(__FUNCTION__); 
} 
foo(); 
 
$obj = new Test; 
$obj->bar(); 
  
?>
Copy after login

Output:

How to get the current function name in PHP? (code example)

##Method 2: Use __METHOD__

Magic constant __METHOD__ can return function and class names.

Code example:

<?php 
class Test  
{ 
    public function foo() { 
        var_dump(__METHOD__); 
    } 
} 
  
  
function bar() 
{ 
    var_dump(__METHOD__); 
} 
  
bar(); 
  
$obj = new Test; 
$obj->foo(); 
  
?>
Copy after login
Output:

How to get the current function name in PHP? (code example)

##Note:

Magic constants __FUNCTION__ and __METHOD__ Retrieving function names is case-sensitive. Related recommendations: "

PHP Tutorial

"The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to get the current function name in PHP? (code example). 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