0

Possible Duplicate:
get current class and method?

How can i find the name of the method i am using in php? I found how to do this in C but not in PHP. I found a Q on here which roughly talked about magic constants (here) but I didn't really get it. In the following example I want $thisMethodName to be 'model_databaseLogin'

EG:

public function model_databaseLogin()
{
$thisMethodName = ... ;
return $this->model_methodCheck( $thisMethodName );
}

Is this possible in php?

Community
  • 1
  • 1
  • @DragoonWraith: I'm sure there is at least several hundreds of duplicates here ;-) – zerkms Aug 02 '12 at 22:41
  • @zerkms: quite probably. Feel kinda dumb for answering first and looking second, but then that was probably John's job, not mine. – KRyan Aug 02 '12 at 22:41

2 Answers2

3

You need the "magic constant" __METHOD__. The magic constant docs should be helpful.

So your code would be:

public function model_databaseLogin() {
    $thisMethodName = __METHOD__;
    return $this->model_methodCheck($thisMethodName);
}
madfriend
  • 2,400
  • 1
  • 20
  • 26
KRyan
  • 7,308
  • 2
  • 40
  • 68
0

The simplest answer is the magic constants to which you refer; specifically __FUNCTION__

These are called "magic" because their value is actually contextually dynamic.

public function model_databaseLogin()
{
$thisMethodName = __FUNCTION__;
return $this->model_methodCheck( $thisMethodName );
}

There is another way, via debug_backtrace(), but that is decidedly less efficient!

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55