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

前端 未结 4 1542
盖世英雄少女心
盖世英雄少女心 2020-12-02 05:12

Is it possible?

function test()
{
  echo \"function name is test\";
}
相关标签:
4条回答
  • 2020-12-02 05:39

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

    0 讨论(0)
  • 2020-12-02 05:44

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

    Example:

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

    Result: MethodA.

    0 讨论(0)
  • 2020-12-02 06:01
    <?php
    
      class Test {
         function MethodA(){
             echo __FUNCTION__ ;
         }
     }
     $test = new Test;
     echo $test->MethodA();
    ?>
    

    Result: "MethodA";

    0 讨论(0)
  • 2020-12-02 06:02

    If you are using PHP 5 you can try this:

    function a() {
        $trace = debug_backtrace();
        echo $trace[0]["function"];
    }
    
    0 讨论(0)
提交回复
热议问题