Can you get a method name from within a method in PHP?

PhpMethodsOop

Php Problem Overview


Is it possible to do something like this?

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

Where method_get_name() returns the method's name?

Php Solutions


Solution 1 - Php

Sure, you want the magic constants.

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

Find out more from the php manual

Solution 2 - Php

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.

Solution 3 - Php

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'.

Solution 4 - Php

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

Solution 5 - Php

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.

Solution 6 - Php

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

Solution 7 - Php

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)

Solution 8 - Php

Why can't you do this?

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexView Question on Stackoverflow
Solution 1 - PhpKevin VaughanView Answer on Stackoverflow
Solution 2 - PhpthesmartView Answer on Stackoverflow
Solution 3 - PhpJan MolakView Answer on Stackoverflow
Solution 4 - PhpAnonymous VisitorView Answer on Stackoverflow
Solution 5 - PhpИлья ЗеленькоView Answer on Stackoverflow
Solution 6 - PhptimdevView Answer on Stackoverflow
Solution 7 - PhpsdotbertoliView Answer on Stackoverflow
Solution 8 - PhpAndrew HareView Answer on Stackoverflow