weird php behaviour with classes and static methods
<p>I maintain an application that uses a (to me) surprising PHP quirk/bug/feature. Consider this code:</p>

<pre><code>&lt;?php
class Bar {
    // called statically
    public function doStuff() {
        print_r($this);
    }
}

class Foo {
    public function main() {
        Bar::doStuff();
    }
}

$foo = new Foo();
$foo-&gt;main();
</code></pre>

<p>Running on PHP 5.2.x, the output is: </p>

<pre><code>Foo Object ( ) 
</code></pre>

<p>That means, although <code>Bar::doStuff()</code> is called statically, it still has access to <code>$this</code> where <code>$this</code> is a reference to the object that called <code>Bar::doStuff()</code>. Never came across that behaviour until recently. Quite evil to rely on this in production code if you ask me.</p>

<p>If you add a <code>static</code> and change the method signature to <code>public static function doStuff()</code> it throws a <code>E_NOTICE:  Undefined variable: this</code> - which seems right to me.</p>

<p>Anyone has an explanation for this behaviour?</p>
