Is it possible to do something like this?
public function something() { $thisMethodName = method_get_name(); }
Where method_get_name()
returns the method's name?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__).
"Method" is basically just the name for a function within a class (or class function). Therefore __METHOD__ consists of the class name and the function name called ( dog::name ), while __FUNCTION__ only gives you the name of the function without any reference to the class it might be in.
Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ . Tip. See also the Userland Naming Guide.
To invoke a method on an object, you simply call the object name followed by "->" and then call the method. Since it's a statement, you close it with a semicolon. When you are dealing with objects in PHP, the "->" is almost always used to access that object, whether it's a property or to call a method.
Sure, you want the magic constants.
function myFunction() { print __FUNCTION__." in ".__FILE__." at ".__LINE__."\n"; }
Find out more from the php manual
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With