While working on a project this morning I discovered what appears to be a bug in PHP when dealing with objects and calling functions statically. I was calling a static function of a class from within an object of a different class. However, I had forgotten the name of the function I needed to call and ended up calling the wrong function. And even though the function I called wasn't declared as static I didn't get any error. Instead $this in the function ended up referring to the calling object.
Here's a short example:
class A {
private $variable;
public function foo() {
$this->variable = 'Hello B!';
}
}
class B {
public function bar() {
A::foo();
}
}
$myB = new B();
$myB->bar();
And the output:
B Object
(
)
B Object
(
[variable] => Hello B!
)
Update:
I've only tried this in version 5.2.6 so far. I suppose it might be the intended behavior but it seems rather odd to me. Though come to think of it, you don't get any errors when calling a non-static function statically from global scope when the function never use $this. In my opinion, these situations should cause a fatal error.