Is it possible?
function test()
{
echo "function name is test";
}
The accurate way is to use the __FUNCTION__ predefined magic constant.
Example:
class Test {
function MethodA(){
echo __FUNCTION__;
}
}
Result: MethodA.
You can use the magic constants __METHOD__ (includes the class name) or __FUNCTION__ (just function name) depending on if it's a method or a function... =)
get_class($this).If you are using PHP 5 you can try this:
function a() {
$trace = debug_backtrace();
echo $trace[0]["function"];
}
__FUNCTION__ returns parent function name (when the current function is included in parent function)<?php
class Test {
function MethodA(){
return __FUNCTION__ ;
}
}
$test = new Test;
echo $test->MethodA();
?>
Result: "MethodA";
<?php
function fun(){
var_dump([
"__FUNCTION__" => __FUNCTION__,
"__METHOD__" => __METHOD__,
]);
}
class C{
function meth(){
var_dump([
"__FUNCTION__" => __FUNCTION__,
"__METHOD__" => __METHOD__,
]);
}
static function static_meth(){
var_dump([
"__FUNCTION__" => __FUNCTION__,
"__METHOD__" => __METHOD__,
]);
}
}
fun();
$o = new C();
$o->meth();
$o->static_meth();
?>
outputs
array(2) {
["__FUNCTION__"]=>
string(3) "fun"
["__METHOD__"]=>
string(3) "fun"
}
array(2) {
["__FUNCTION__"]=>
string(4) "meth"
["__METHOD__"]=>
string(7) "C::meth"
}
array(2) {
["__FUNCTION__"]=>
string(11) "static_meth"
["__METHOD__"]=>
string(14) "C::static_meth"
}
Fwiw if you want to call yourself without knowing/caring about your function name, you'd do
(__METHOD__)();
or perhaps
(__METHOD__)(...func_get_args());
function name1()then use name1 again inside), would save lots of time if you have the same template for lots of functions.