5

So, I'm looking for how to get the caller's method name. I never saw it and I don't know if it exists.

Example:

<?php 
  class father {
    public function db_select(){
      echo method that call me;
    }
  }

  class plugin extends father {
    public function select_plugin(){
      $query = $this->db_select();
    }
   }

?>

All I want is to write the method's name that calls db_select() method, inside db_select().

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 9
    You shouldn't ever need to do this: if you do, then it's bad design - http://stackoverflow.com/questions/2110732/how-to-get-name-of-calling-function-method-in-php – Mark Baker Dec 18 '15 at 16:24
  • 1
    Thks @MarkBaker , I think is not a bad design. is to make a smart cache database system. – Jean Victor Dec 18 '15 at 16:32
  • 4
    If your code is dependent on knowing where it is called from, then it's bad design, not matter what purpose you're trying to achieve – Mark Baker Dec 18 '15 at 16:34
  • @MarkBaker Well, I don't think so. 'cause inside db_select() will be generated a file with father's method name. But if you know how I can do it better than it please say :) – Jean Victor Dec 18 '15 at 16:44
  • This kind of thing can be useful for error messages. – Barmar Dec 18 '15 at 16:45
  • 1
    Create a method (or a static property) in each of your classes that extends `father` calling it something like `getName()` or `name`.... then `db_select()` can call that, or reference it using `late static binding`.... or even just enforce that your plugin must pass a filename argument (or even a logfile handler with a file already opened) to db_select – Mark Baker Dec 18 '15 at 16:48
  • Duplicate of http://stackoverflow.com/questions/2110732/how-to-get-name-of-calling-function-method-in-php. seems to me what you should need is only the instantiated class name `get_class($this)` – Adam Dec 18 '15 at 16:49

2 Answers2

7

I think debug_backtrace could do the trick.

It gives you a backtrace of a function call. Observe this result then you'll have to figure out how to grab the function name you want. But the information is there.

<?php 
  class father {
    public function db_select(){
      echo debug_backtrace()[1]['function'];
      print_r(debug_backtrace());     
    }
  }

  class plugin extends father {
    public function select_plugin(){
      $query = $this->db_select();
    }
   }

?>
Dan
  • 10,614
  • 5
  • 24
  • 35
-2

Disclaimer: i do not use PHP.

@Jean Victor -- it would help if you (a) explained in more detail WHY you want this and (b) shared some information about your PHP platform.

"Caller Information (C# and Visual Basic)"

"By using Caller Info attributes, you can obtain information about the caller to a method. You can obtain file path of the source code, the line number in the source code, and the member name of the caller. This information is helpful for tracing, debugging, and creating diagnostic tools."

Unfortunately, taking advantage of this feature may be a major challenge in PHP.

First, if you are using a .NET platform, you would have to discover whether the version of PHP that you are using takes advantage of the System.Runtime.CompilerServices Namespace because it "provides functionality for compiler writers who use managed code to specify attributes in metadata that affect the run-time behavior of the common language runtime".

ALTERNATIVE: use an ugly kludge (works only for functions that you own or can modify).

ensure that the first argument of ALL of your functions is a GUID.

# id of  myFunctionABC f4f866c7728040a6bf22c7600c64a7ee
function myFunctionABC ($whoCalledMe) {

# Note:  myFunctionABC would use the value passed
#        via $whoCalledMe in error messages.



# BUT if from within myFuntionABC some other
# function is called, the above GUID would be used:
#
#     foo("f4f866c7728040a6bf22c7600c64a7ee")

}

Yes, it's a very, very ugly kludge.
FWIW

edit:
when i wrote this, i was not aware of debug_backtrace.
debug_backtrace should also work on non-.NET platforms.
end edit.

gerryLowry
  • 2,626
  • 5
  • 35
  • 44