7

I really hate to write this question because I'm kind-of "research guy" and, well, I always find what I'm searching for... But this one bugs me a lot and I can't find the answer anywhere... So, here it goes:

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

Edit:

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

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

That's all OK with this if I call the function I'll get (let's assume that method is in System\Helpers class) - "System\Helpers::someFunction". But what I want is 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"
}

I hope this cleared my question a bit :)

  • Why not simply `Text::class . '::loadLanguageCache'`? I can't think of any reason why you'd *need* anything else, even if it may be nice to have. – deceze Nov 30 '13 at 15:46
  • Well if there's no such a thing that I want, I guess I'm stuck with `Text::class . '::loadLanguageCache'`... But it would be very nice to have :) – Vytautas Lozickas Nov 30 '13 at 15:56
  • I'm not entirely clear what you have now - in the example you cite, the line of code `$thatFunctionName = Helpers::someFunction::method;` already includes the answer you want, so you could just type `$thatFunctionName = 'someFunction';`. Presumably there is a more complex scenario you're thinking of where this wouldn't be the case, but I'm struggling to imagine it. – IMSoP Nov 30 '13 at 15:56
  • 1
    `$thatFunctionName = Helpers::someFunction::method;` - that was the imaginary line, because `method` thing does not exist. Of course I could just type: `$thatFunctionName = 'System\Helpers::someFunction';` but that is just not nice because of refactoring, possible namespace changes, class name changes and so on :) – Vytautas Lozickas Nov 30 '13 at 16:06
  • 1
    I think the reason this wouldn't be useful is that methods rarely need to be represented in a fully-qualified form like that. For a callback, for instance, you'd use an array: `[Helpers::class, 'someFunction']`; and for an instance (non-static) method, a string with `::` in it would be just plain wrong. So take the qualified class name and, for your purposes, append `'::someFunction'` as a hard-coded string, since it's hard-coded even in your imaginary syntax. – IMSoP Nov 30 '13 at 16:44
  • 1
    I want that whole string because I send it as an AJAX parameter and I don't want two separate strings for class and method (PHP calls it to handle request using reflection). Concatenating that string is all fine, I just thought there is kind of "nicer" way :) – Vytautas Lozickas Nov 30 '13 at 17:09
  • 2
    i'm looking for this because i use laravel and i don't want the controller actions (what methods from what classes to call) in my routes to be expressed inside strings like 'UserController@authenticate'... using a string, i don't have the benefits of code refactoring in my IDE... would like to know if you found a way around this. – Alexandre Martini Dec 08 '14 at 03:46
  • Unfortunately i have not found that "nice" way just because it does not exist and probably will never be implemented in php... +1 for Laravel - such a great framework :) – Vytautas Lozickas Dec 21 '14 at 01:02
  • 2
    Such an underrated question. It is these fine details that make you love and adore your code. It can be much more difficult to find references to a method if you hard-code strings like that, but an equivalent of ::class as ::method instead would be such a blessing for fine code. I have not found the solution online yet that we seem to look for, but I think it can also be difficult to formulate this issue--specifically--as a simple, searchable question. Any luck, 6 years later? – Hello World Nov 21 '20 at 11:02
  • @HelloWorld Not really I just bountied a question which is essentially asking for the same thing and someone drove me to this question (https://stackoverflow.com/questions/66238766/php-callback-is-there-an-equivalent-for-class-for-a-method-of-a-class?noredirect=1). We have is_callable and call_user_func but not exactly the great thing which is requested here. Do you think it is worth a PHP feature request? – Blackbam Feb 19 '21 at 15:47

1 Answers1

3

You must use the magic constant, you can read more about at Magic constants in php.net

__METHOD__

Outside of the class you must use Reflection as is explained at ReflectionClass documentation:

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

Returns:

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(9) "getMethod"
  ["class"]=>
  string(15) "ReflectionClass"
}
mcuadros
  • 4,098
  • 3
  • 37
  • 44
  • I know about this magic constant, but it works inside a method, and I need this to work "outside" the method... – Vytautas Lozickas Nov 30 '13 at 15:35
  • 1
    Yeah, that's what I want, and I know about Reflection too. But the whole idea of a question was the simplicity, to just use Foo::someMethod::method to get that string (as easily as you can do with Foo::class). :) – Vytautas Lozickas Nov 30 '13 at 15:50
  • But the class name not is obvious but the method yes, just $methodPath = Helpers::class . 'theMethod'; you cannot use AS in methods. :/ – mcuadros Nov 30 '13 at 15:58