PHP Sadness

No good way to modify private method behavior in a testing environment

$ cat test.php
<?php
class super {
  public function a() { $this->b(); }
  private function b() { print "super\n"; }
}

class sub extends super {
  private function b() { print "sub\n"; }
}

$obj = new sub();
$obj->a();

$ php test.php
super

The point of private methods is to prevent what other code can call them. However, in PHP, you also cannot override private methods in a subclass. This is especially important when, for example, trying to build mocks in unit tests.

The solution here is not to simply make the method php protected - this entirely changes the restrictions about how the function can be called, which is probably not what the author intended.