omg
omg

Reputation: 139862

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

Is it possible?

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

Upvotes: 277

Views: 153195

Answers (4)

Snehal K
Snehal K

Reputation: 29

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

Result: "MethodA";

Upvotes: 2

Silfverstrom
Silfverstrom

Reputation: 29322

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

Example:

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

Result: MethodA.

Upvotes: 452

Kevin Newman
Kevin Newman

Reputation: 2447

If you are using PHP 5 you can try this:

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

Upvotes: 20

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45721

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

Upvotes: 107

Related Questions