Static within non-static method is shared between instances
<p>I've come across some unexpected behavior with static variables defined inside object methods being shared across instances. This is probably known behavior, but as I browse the PHP documentation I can't find instances of statically-defined variables within object methods.</p>

<p>Here is a reduction of the behavior I've come across:</p>

<pre><code>&lt;?php

class Foo {
  public function dofoo() {
    static $i = 0;
    echo $i++ . '&lt;br&gt;';
  }
}

$f = new Foo;
$g = new Foo;

$f-&gt;dofoo(); // expected 0, got 0
$f-&gt;dofoo(); // expected 1, got 1
$f-&gt;dofoo(); // expected 2, got 2

$g-&gt;dofoo(); // expected 0, got 3
$g-&gt;dofoo(); // expected 1, got 4
$g-&gt;dofoo(); // expected 2, got 5
</code></pre>

<p>Now, I would have expected <code>$i</code> to be static <em>per instance</em>, but in reality <code>$i</code> is shared between the instances. For my own edification, could someone elaborate on why this is the case, and where it's documented on php.net?</p>
