__FUNCTION__
Starting with version 4.3.0 of PHP there is the magic constant % with% does exactly this function:
class Foo {
public function method()
{
echo __FUNCTION__ ;
}
}
$f = new Foo();
$f->method();
The above code returns __FUNCTION__
. See working at Repl.it or Ideone >.
method
There is still the constant __METHOD__
, starting with version 5.0.0 of PHP, which can be used next to classes and the difference of it to __METHOD__
is that __FUNCTION__
returns the name of the class too. p>
class Foo {
public function method()
{
echo __METHOD__ ;
}
}
$f = new Foo();
$f->method();
The output will be:
Foo::method
Including the class name __METHOD__
.
It is interesting to note that when the executed method is inherited from a parent class, the class name returned by Foo
will also be from the parent class, since it is where the method is defined.
class Bar
{
public function method()
{
echo __METHOD__;
}
}
class Foo extends Bar {}
$foo = new Foo();
$foo->method();
The output will be __METHOD__
even if the object is an instance of Bar::method
.