Get the name that the function is called as CFML equivalent getFunctionCalledName ()

Here's a sample CFML code that I'm using as a baseline (also on github @ getFunctionCalledName.cfm ):

function f(){
    echo("#getFunctionCalledName()#() called<br>");


g = f;



F () is called

G () is called

Note that this getFunctionCalledName()

returns the name of the reference used to call the function, not just the name of the function.

I'm trying to see if there is an equivalent in PHP. Here's my test ( testFunction.php on GitHub):

I know the magic constant __FUNCTION__


function f()
    echo sprintf("Using __FUNCTION__: %s() called<br>", __FUNCTION__);

echo "<hr>";

$g = "f"; // as someone points out below, this is perhaps not analogous to the g = f in the CFML code above. I dunno how to simply make a new reference to a function (other than via using function expressions instead, which is not the same situation


However, this returns f

in both cases.

I wrote similar code trying debug_backtrace()

(see testDebugBackTrace.php ) but also referencing the function as f


This is fine and I understand why they both do it. However I am wondering if there is any PHP equivalent getFunctionCalledName()

. I am in a hurry to add this just for a research exercise: I am currently moving from CFML to PHP and it just showed up when I was demonstrating something on my blog.


source to share

2 answers

Your code $g = "f";

doesn't actually copy the function into another variable, it just creates a string reference for the same function. The following is more similar to the CFML you provide:

$x = function() {
    echo __FUNCTION__;

$v = $x;


The above code just outputs {closure}

, since PHP does not assign a formal name to anonymous functions. So the answer is no, this is not possible in PHP.



I don't think what you want is possible in PHP without something like a runkit extension. It has the functionality to create a copy of the function under a new name. I haven't tried this myself as I couldn't find the version of the Windows extension.

function f() {
  echo sprintf("Using __FUNCTION__: %s() called<br>", __FUNCTION__);
runkit_function_copy('f', 'g');




All Articles