Friday, February 27, 2009

Fun with PHP implicit scoping on static calls

class a {
public $a = "yay!";

func call_b()
{
b::b();
}
}

class b {
function b()
{
echo $this->a;
}
}

$a= new a();
$a->call_b();


Prints:
yay!

This is because if you statically call a method that is not declared as static in PHP, it inherits the class scope of the caller. Note that if you had declared function b as a static method, you'd get a different behavior.

No comments: