I know the difference between these two, and I've checked the manual.
I am still confused about some points. I can use __FUNCTION__
in a method; it represents the method name. This is clear.
Why can I use __METHOD__
in a function?
This also just represents the function name.
class dog {
private $name = 'dog';
public static function name() {
echo __METHOD__;
echo "\n";
echo __FUNCTION__;
echo "\n";
}
}
function test() {
echo __FUNCTION__;
echo "\n";
echo __METHOD__;
}
dog::name();
test();
This code outputs:
dog::name
name
test
test
"Method" is basically just the name for a function within a class (or class function). Therefore __METHOD__
consists of the class name and the function name called (dog::name
), while __FUNCTION__
only gives you the name of the function without any reference to the class it might be in.
When __METHOD__
is called outside of a class it's the same as __FUNCTION__
because there is no class part to be used as a prefix. You can use __METHOD__
outside of a class because it's a magic constant and they're always available (at worst case they will return empty string).