I'm using static properties quite a lot, often in combination with static classes. This is, where I often encounter a major drawback in PHP. It is not possible to write code like this:
<?php
class Foo {
private static $bar = new Bar();
public static function getBar() {
return self::$bar;
}
public static function modifyBar() {
self::$bar->doSomething();
}
}
?>
In PHP. it is not possible to initialize a property with an object.
As long as a constructor is involved, this is no problem, as the workaround is to initialize this properties in the constructor:
<?php
class Foo {
private $bar;
public function __construct() {
$this->bar = new Bar();
}
}
?>
But how could this be solved, when you never create an instance of Foo but only use static method calls?
Continue reading "My wishlist for PHP6, pt4: static initializers"