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

PhpFunction

Php Problem Overview


Is it possible?

function test()
{
  echo "function name is test";
}

Php Solutions


Solution 1 - Php

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

Example:

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

Result: MethodA.

Solution 2 - Php

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... =)

Solution 3 - Php

If you are using PHP 5 you can try this:

function a() {
    $trace = debug_backtrace();
    echo $trace[0]["function"];
}

Solution 4 - Php

<?php
   
  class Test {
     function MethodA(){
         echo __FUNCTION__ ;
     }
 }
 $test = new Test;
 echo $test->MethodA();
?>

Result: "MethodA";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionomgView Question on Stackoverflow
Solution 1 - PhpSilfverstromView Answer on Stackoverflow
Solution 2 - PhpPatrikAkerstrandView Answer on Stackoverflow
Solution 3 - PhpKevin NewmanView Answer on Stackoverflow
Solution 4 - PhpSnehal KView Answer on Stackoverflow