Friday, October 10, 2025
HomeLanguagesHow to get current function name in PHP?

How to get current function name in PHP?

The function name can be easily obtained by the __FUNCTION__ or the __METHOD__
magic constant.

Method 1 (Prints function name):
__FUNCTION__ is used to resolve function name or method name (function in class).

Example:




<?php
class Test {
  
    public function bar() {
        var_dump(__FUNCTION__);
    }
}
  
function foo() {
    var_dump(__FUNCTION__);
}
  
// Must output string(3) 'foo' 
foo();
  
$obj = new Test;
  
// Must output string(3) 'bar'
$obj->bar();


Output:

string(3) "foo"
string(3) "bar"

Method 2 (Prints function and class name):
using __METHOD__.




<?php
  
class Test 
{
    public function foo() {
        var_dump(__METHOD__);
    }
}
  
  
function bar()
{
    var_dump(__METHOD__);
}
  
// Same As __FUNCTION__
bar();
  
$obj = new Test;
  
// Output the fully qualified method name "ClassName::MethodName"
$obj->foo();


Output:

string(3) "bar"
string(9) "Test::foo"

Note: this code is tested with php7.1

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS