I would like to know if is a good practice to declare a property of an object like:
$this->name = $name;
out of the function __construct
I am trying to build an object with data from a database table. But this object will be build only if the id is registered. I know that the __construct function always return an object so I can not get a false return. So I tried the following:
//test.php
$mod = new item($id);
if($mod->validate()) {
$item = $mod;
}
class item {
  protected $id;
    public function __construct($id)    {
        $this->id = $id;
    }
public function validate() {
        $db = new db('restaurants_items_modifiers');
        if($mod = $db->get($this->id)) {
            $this->price = $mod['price'];
            $this->name = $mod['name'];
            $this->desc = $mod['desc'];
            return true;
        } else {
            return false;
        }
    }
}
This will work but is a good practice to do it like this? or I should declare everything on the __construct function?