Thursday 7 May 2009

Admin pages fixed

So, my URL flyweight change had some unexpected problems. Basically, I switched a normal object method into a static one. Any problems with object access like $url->getUrl() should throw an error, right? I should be able to find and fix any old method access. Not so in PHP.

class Foo {
public static function bar() {
echo "foo\n";
}
}
class Zap extends Foo {
public static function bar() {
parent::bar();
}
}
$z = new Zap();
$z->bar();


In PHP, the parent:: token is ambiguous. It works equally well in static and object calling contexts. Basically, it passes along the calling context to the next function up. If you had declared bar() previously as non-static, you wouldn't notice any errors in this setting. But the behavior of calling Zap::bar() would change drastically.

The point is, if you're changing a method from non-static to static, you need to be aware of the internals of all the functions which subclass it. If they call parent::bar(), the result could be unexpected. In the above example, $z would not be modified at all, and no errors, warnings, nor E_STRICTS would be thrown.

No comments: