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

Is it possible to do something like this?

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

Where method_get_name() returns the name of the method?

+62
methods oop php
Sep 15 '09 at 2:31
source share
8 answers

Of course you need magic constants.

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

Learn more from the php manual

+100
Sep 15 '09 at 2:38
source share

Although you can use the magic constant __METHOD__ I highly recommend checking PHP reflection . This is supported in PHP5.

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

Then you can do things like signature verification, etc.

+22
Sep 15 '09 at 2:48
source share

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

+11
Feb 04 '10 at 19:55
source share

Using __FUNCTION__ is the way instead:

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

which is somewhat frightening to add a variable and memory to store the method name as a string and duplicate what already exists, so adding resources to it unnecessarily (if you do this for a large library with many methods, this is of great importance).

The magic constants in PHP are not guaranteed to change, while this approach will require applicable editing if the method name has been changed, thereby presenting a potential for inconsistency (note that I really said that this simply means unnecessary editing if a magic constant was used instead).

Time and effort to name a variable, enter the type of the method as a string assigned to this unnecessary variable, and, of course, correctly referring to the name of the variable, which is the motivation for PHP supplying magic constants to start (and refuting any __FUNCTION__ requirement __FUNCTION__ not required )

+2
Apr 27 '13 at 15:40
source share

Hackish, but you can also dig it from the return value of debug_backtrace ().

+1
Sep 15 '09 at 2:39
source share

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 it doesn't care about replacing the method name inside the method if I want to change it.

0
Oct 27 '18 at 17:11
source share

It is possible to skip late bindings this way. If class B extends class A: METHOD always returns the part "Class ::" related to A ("A :: methodName")

Solution: static :: class. "::". FUNCTION

This way you get a class reference for the current working class ("B :: methodName")

0
Jul 15 '19 at 11:56 on
source share

Why can't you do this?

 public function something() { $thisMethodName = "something"; } 
-6
Sep 15 '09 at 2:37
source share



All Articles