Can anyone explain why is the get_class function returning different values below? Specifically, what is it supposed to do when it is called in a base class and when it is called in a derived class?
abstract class bar {
public function __construct()
{
var_dump(get_class($this)); //prints 'foo'
var_dump(get_class()); // prints 'bar'
}
}
class foo extends bar {
}
new foo;
It seems quite well explained in the documentation, but here it is:
get_class($instance)returns the class of the$instanceinstance, regardless of where you’re calling it;get_class($this)does behave the same way, returning the class of$this.get_class()returns the class where the method calling it is defined, thus it returnsbarin your example, as that is where__construct()is defined (even though you’re calling it through inheritance).