I'm having a hard time understanding the pseudo variable $this. Take for example the following code:
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ")\n";
} else {
echo "\$this is not defined.\n";
}
}
}
$a = new A();
$a->foo();
A::foo();
?>
It will output:
$this is defined (A)
$this is not defined.
OK, this output makes sense to me, since $a->foo refers to an instance and A::foo refers to a class without an instantiation. But take this other example, if we append the following code to the first example:
<?php
class B
{
function bar()
{
A::foo();
}
}
$b = new B();
$b->bar();
B::bar();
?>
It will output this:
$this is defined (B)
$this is not defined.
I can understand the second output, because B::bar(); refers to a class, not an instance. But I don't get the first output. Why is $this instantiated if the method calls A::foo();, which is a class and should not be instantiated, and even worse, why would $this be an instance of B and not of A?
Thanks for your attention!