Is it possible to set a property of a class as a object?
Like:
class User {
public $x = \"\";
public $y = new ErrorVO();
public $w = new
Just my preferred solution, even if the others already explain everything: Injection
class A {
public $a;
public function __construct (ErrorVO $a) {
$this->a = $a;
}
}
This keeps the class testable and allows to replace the wanted ErrorVO
-implementation very easy. Of course you can combine both solutions into one
class A {
public $a;
public function __construct (ErrorVO $a = null) {
$this->a = is_null($a) ? new ErrorVO : $a;
}
}
Minor Update: In the meantime you can write the second example like this
class A {
public $a;
public function __construct (ErrorVO $a = null) {
$this->a = $a ?: new ErrorVO;
}
}
It's slightly more compact and imo it makes the intention more clear. Compare the ?:
-operator with MySQLs COALESCE