$attribute; if($this->allowEmpty && $this->isEmpty($value)){ return; } if(is_array($value)){ foreach($value as $key=>$attr){ $value[$key] = $attr instanceof MongoId ? $attr : new MongoId($attr); } $object->$attribute = $value; }else{ $object->$attribute = $object->$attribute instanceof MongoId ? $object->$attribute : new MongoId($object->$attribute); } } }