note 116025 deleted from language.constants.predefined by peehaa
| From: | peehaa@php.net | Date: | Mon, 22 May 2017 07:24:26 +0000 |
| Subject: | note 116025 deleted from language.constants.predefined by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-209081@lists.php.net to get a copy of this message | ||
Note Submitter: eldy at destailleur dot fr
----
Using __METHOD__ always return class name the function belong to. But in some cases (like to make
log output), if class B1, B2, B3 inherit class A and call a method of class A, you may would like to
see into log which class among B1, B2, B3 is calling.
For example you would like to see:
B1::commond_method_inside_framework
and not
A::commond_method_inside_framework
because you know that common_method_inside_framework is inside common inherited class A.
To solve this, replace __METHOD__ with get_class($this).'::'.__FUNCTION__
class A
{
function commond_method_inside_framework()
{
echo "This is class " . get_class($this).'::'.__FUNCTION__.'
'. __METHOD__.' '."\n";
}
}
class B1 extends A
{
function commond_method_inside_caller()
{
echo "This is class " . get_class($this).'::'.__FUNCTION__.'
'. __METHOD__.' '."\n";
}
}
$a = new A();
$a->commond_method_inside_framework();
$b = new B1();
$b->commond_method_inside_framework();
$b->commond_method_inside_caller();
?>
Result will be
This is class A::commond_method_inside_framework A::commond_method_inside_framework
This is class B1::commond_method_inside_framework A::commond_method_inside_framework
This is class B1::commond_method_inside_caller B1::commond_method_inside_caller