PHP Sadness

(intended) Static variables in methods are bound to all instances of the class

Static variables in methods are bound to the class, not instances of the class.

That is, if a method has a static variable, it is global to all instances of the class. I had expected this to mean "static to that instance of that method", rather than meaning "static to the class", but it turns out this behavior is intentional.

$ cat staticvar.php
<?php
class StaticVar {
  public function getNext() {
    static $curNumber = 0;
    return $curNumber++;
  }
}

$a = new StaticVar();
$b = new StaticVar();

echo $a->getNext()."\n";
echo $a->getNext()."\n";
echo $a->getNext()."\n";

echo $b->getNext()."\n";
echo $b->getNext()."\n";
echo $b->getNext()."\n";

$ php staticvar.php
0
1
2
3
4
5