Timeline for PHP How to get a method name with a class and namespace path as a string?
Current License: CC BY-SA 3.0
16 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 24, 2024 at 23:46 | answer | added | Roemer | timeline score: 0 | |
| Feb 19, 2021 at 15:47 | comment | added | Blackbam | @HelloWorld Not really I just bountied a question which is essentially asking for the same thing and someone drove me to this question (stackoverflow.com/questions/66238766/…). 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? | |
| Nov 21, 2020 at 11:02 | comment | added | Hello World | 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? | |
| Dec 21, 2014 at 1:02 | comment | added | Vytautas Lozickas | 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 :) | |
| Dec 8, 2014 at 3:46 | comment | added | Alexandre Martini | 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. | |
| Nov 30, 2013 at 17:09 | comment | added | Vytautas Lozickas | 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 :) | |
| Nov 30, 2013 at 16:44 | comment | added | IMSoP |
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.
|
|
| Nov 30, 2013 at 16:06 | comment | added | Vytautas Lozickas |
$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 :)
|
|
| Nov 30, 2013 at 15:56 | comment | added | IMSoP |
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.
|
|
| Nov 30, 2013 at 15:56 | comment | added | Vytautas Lozickas |
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 :)
|
|
| Nov 30, 2013 at 15:53 | history | edited | Vytautas Lozickas | CC BY-SA 3.0 |
added 769 characters in body
|
| Nov 30, 2013 at 15:47 | history | edited | Vytautas Lozickas | CC BY-SA 3.0 |
added 769 characters in body
|
| Nov 30, 2013 at 15:46 | comment | added | deceze♦ |
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.
|
|
| Nov 30, 2013 at 15:34 | review | First posts | |||
| Nov 30, 2013 at 15:36 | |||||
| Nov 30, 2013 at 15:31 | answer | added | mcuadros | timeline score: 3 | |
| Nov 30, 2013 at 15:19 | history | asked | Vytautas Lozickas | CC BY-SA 3.0 |