81

Is it possible to do something like this?

public function something() {
    $thisMethodName = method_get_name(); 
}

Where method_get_name() returns the method's name?

alex
  • 479,566
  • 201
  • 878
  • 984

8 Answers8

138

Sure, you want the magic constants.

function myFunction() { print __FUNCTION__." in ".__FILE__." at ".__LINE__."\n"; }

Find out more from the php manual

Kevin Vaughan
  • 14,320
  • 4
  • 29
  • 22
  • 45
    and just to be cheeky, the magic constant for method name is: `__METHOD__` – Anthony Sep 15 '09 at 02:43
  • 46
    Late to the game, but I was surprised to find that `__METHOD__` actually returns `Class::method` so `__FUNCTION__` specifically returns just the method name... for anyone else who *still* doesn't RTFM like me. – Chords Nov 12 '13 at 22:19
28

While you can use the magic constant __METHOD__ I would highly recommend checking out PHP's reflection. This is supported in PHP5.

$modelReflector = new ReflectionClass(__CLASS__);
$method = $modelReflector->getMethod(__METHOD__);

You can then do kick-ass stuff like inspect the signature, etc.

Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
thesmart
  • 2,993
  • 2
  • 31
  • 34
  • When you say "inspect the signature", you mean this? http://www.php.net/manual/en/function.openssl-verify.php – Hector Ordonez Dec 25 '13 at 21:14
  • @MrMe No, a method signature (not to be confused with an authentication signature) like this: http://en.wikipedia.org/wiki/Type_signature – thesmart Dec 26 '13 at 23:33
  • Oh alright, it's basically Type Hinting http://stackoverflow.com/questions/2161040/is-method-signature-in-php-a-must-or-should Thanks! – Hector Ordonez Dec 27 '13 at 10:35
14

As smartj suggested you can try the magic constant __METHOD__, but remember that this will return a string containing also your class name, i.e. 'MyClass::something'. Using __FUNCTION__ instead will return 'something'.

Jan Molak
  • 4,426
  • 2
  • 36
  • 32
3

Using __FUNCTION__ is the way to go instead of:

 public function something() {
     $thisMethodName = "something";
 }

which is flawed in several ways adding a variable and memory to store the method name as a string and duplicating what already exists, thus unnecessarily adding resources used, (if you do this for a large library with many methods, it matters greatly).

Magic constants in PHP are guaranteed not to change, while this approach would require applicable editing if the method name were changed, thus introducing the potential for an inconsistency (note, I did say potentially, meaning simply it is an otherwise unnecessary edit if the magic constant were used instead).

The time and effort to name a variable, re-type the method name as a string assigned to that unnecessary variable and of course properly referencing the variable name, which is the motivation for PHP supplying magic constants to begin with (and refuting any claim __FUNCTION__ is unnecessary).

alex
  • 479,566
  • 201
  • 878
  • 984
3

With __FUNCTION__ i can use this:

protected static function getUserResponseByAccessTokenRequestOptions(string $myParam): array
{
    return array_merge(parent::{__FUNCTION__}($myParam), [
        'myValue' => '123'
    ]);
}

instead of this:

protected static function getUserResponseByAccessTokenRequestOptions(string $myParam): array
{
    return array_merge(parent::getUserResponseByAccessTokenRequestOptions($myParam), [
        'myValue' => '123'
    ]);
}

And does not care about replacing the name of the method inside method if I want to change it.

  • This is a great reference, also works for cases like `self::{self::$some_method_name}();` :-) where is the documentation for it? (I know I've seen it somewhere over the years, just can't remember the exact name for it) (Generally I know it's a combination of `Variable functions` and `Variable variables` ) – jave.web Feb 25 '21 at 01:12
3

Perhaps missing late bindings in this way.

when class B extend class A:

__METHOD__ always return the "className::" part referred to A (A::methodName)

Solution is:

static::class."::".__FUNCTION__

In this way you obtain the class reference to current working class (B::methodName)

sdotbertoli
  • 131
  • 1
  • 4
1

Hackish, but you could also probably dig it out of the debug_backtrace() return value.

timdev
  • 61,857
  • 6
  • 82
  • 92
  • This might be the way to go if you have a common logging function that needs the name of the function that called it. – user9645 Jan 28 '19 at 12:46
-7

Why can't you do this?

public function something() {
    $thisMethodName = "something";
}
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635