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

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

青灯夜游
青灯夜游Original
2019-03-11 13:11:476188browse

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

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

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!

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