How do I get the function name inside a function in PHP?


Question

Is it possible?

function test()
{
  echo "function name is test";
}
1
217
1/8/2018 1:57:24 PM

Accepted Answer

The accurate way is to use the __FUNCTION__ predefined magic constant.

Example:

class Test {
    function MethodA(){
        echo __FUNCTION__;
    }
}

Result: MethodA.

361
4/11/2018 4:17:36 PM

You can use the magic constants __METHOD__ (includes the class name) or __FUNCTION__ (just function name) depending on if it's a method or a function... =)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon