PHP How to get a method name with a class and namespace as a string?

I really like to write this question because I'm a kind, “scientific guy,” and, well, I always find what I was looking for ... But this one bothers me very much, and I can’t find the answer anywhere ... So , here it is:

As the title says, I need to get the method name with the final class and namespace as a string. I mean something like this: "System\Language\Text::loadLanguageCache". As you know, you can get the class name (with a full namespace) by typing, i.e.: Text::class, and it returns "System\Language\Text", but is there any way to get this for the method? Something like: Text::loadLanguageCache::functionto get the string "System\Language\Text::loadLanguageCache":?

Edit:

I think I should explain this further ... I know about the magic constant __METHOD__, but the problem is that it works inside the called method, and I need it "outside the method". Take this as an example:

//in System\Helpers
function someFunction()
{ return __METHOD__; }

Everything is fine with this if I call the function that I get (let it be assumed that the method is in the class System\Helpers) - "System\Helpers::someFunction". But I want this:

//in System\Helpers
function someFunction()
{ //some code... whatever }

// somewhere not in System\Helpers
function otherFunction()
{
    $thatFunctionName = Helpers::someFunction::method //That imaginary thing I want

    $thatClassName = Helpers::class; //this returns "System\Helpers"
}

Hope this eased my question a bit :)

+4
source share
1 answer

You must use magic constant, you can learn more about magic constants in php.net

__METHOD__

Reflection, ReflectionClass:

<?php
$class = new ReflectionClass('ReflectionClass');
$method = $class->getMethod('getMethod');
var_dump($method);
?>;

:

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(9) "getMethod"
  ["class"]=>
  string(15) "ReflectionClass"
}
+2

All Articles