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 array();
}
In the constructor, yes.
class User
{
public $x = "";
public $y = null;
public $w = array();
public function __construct()
{
$this->y = new ErrorVO();
}
}
Edit
KingCrunch made a good point: You should not hard-code your dependencies. You should inject them to your objects (Inversion of Control (IoC)).
class User
{
public $x = "";
public $y = null;
public $w = array();
public function __construct(ErrorVO $y)
{
$this->y = $y;
}
}
new User(new ErrorVD());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With