Unfortunately, the following is not possible in PHP:
class SpamHam {}
class EggBaz {
function EggBaz($var) {
}
}
class FooBar {
public $fields = array( new SpamHam, new EggBaz(9) );
}
Java, for example, allows the following:
class FooBar {
public static Object fields[];
static {
fields = new Object[] { new SpamHam(), new EggBaz(9) };
}
}
Is there something similiar in PHP? I want to avoid constructions like this:
class FooBar {
static $fields = null;
static function initFields() {
static::$fields = array( new SpamHam, new EggBaz(9) );
}
}
FooBar::initFields();
In short: sorry, no.
Class properties can only be initialized with constant values, and there’s nothing like a static constructor.