tags:

views:

1435

answers:

6

Is it possible to do something like this?

public function something() {

    $thisMethodName = method_get_name(); // I made that function up, but is there some way of doing what it describes?

}

Thanks

+1  A: 

Why can't you do this?

public function something() {
    $thisMethodName = "something";
}
Andrew Hare
Completely unnecessary when `__FUNCTION__` is available.
too much php
Fair enough - but in the same spirit I could content that `__FUNCTION__ is unnecessary :)
Andrew Hare
+13  A: 

Sure, you want the magic constants.

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

Find out more from the php manual

Kevin
and just to be cheeky, the magic constant for method name is: `__METHOD__`
Anthony
+1  A: 

Hackish, but you could also probably dig it out of the debug_backtrace() return value.

timdev
+7  A: 

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.

thesmart
+1  A: 

I think aswering such questions should include some mentoring. _ FUNCTION _ is supposed to be used in debugging purposes. Using it in other cases in questionable. I suggest author to describe the task he wants to achieve with this "do I know what my name is" thing. And we will help him invent some more acceptable solution.

FractalizeR
+3  A: 

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

Jan Molak