The Note You're Voting On
mail dot temc at gmail dot com ¶ 14 years ago
People seem to mix up what __METHOD__, get_class($obj) and get_class() do, related to class inheritance.
Here's a good example that should fix that for ever:
<?php
class Foo {
function doMethod(){
echo __METHOD__ . "\n";
}
function doGetClassThis(){
echo get_class($this).'::doThat' . "\n";
}
function doGetClass(){
echo get_class().'::doThat' . "\n";
}
}
class Bar extends Foo {
}
class Quux extends Bar {
function doMethod(){
echo __METHOD__ . "\n";
}
function doGetClassThis(){
echo get_class($this).'::doThat' . "\n";
}
function doGetClass(){
echo get_class().'::doThat' . "\n";
}
}
$foo = new Foo();
$bar = new Bar();
$quux = new Quux();
echo "\n--doMethod--\n";
$foo->doMethod();
$bar->doMethod();
$quux->doMethod();
echo "\n--doGetClassThis--\n";
$foo->doGetClassThis();
$bar->doGetClassThis();
$quux->doGetClassThis();
echo "\n--doGetClass--\n";
$foo->doGetClass();
$bar->doGetClass();
$quux->doGetClass();
?>
OUTPUT:
--doMethod--
Foo::doMethod
Foo::doMethod
Quux::doMethod
--doGetClassThis--
Foo::doThat
Bar::doThat
Quux::doThat
--doGetClass--
Foo::doThat
Foo::doThat
Quux::doThat