my question is based on this "principles"
- do not throw exceptions in a constructor
- single responsibility / seperation of conerns
- use logic exceptions for broken contracts and runtime exceptions for everything else
so whats the best way to validate the contract and whos job is it?
lets assume there is a class "world"
class world {
public function __construct($sName, array $aSomething) {}
}
where do i go from here? should i validate the params and throw an exception in the constructor? should i inject an valdator?
public function __construct($sName, array $aSomething, Validator $oWorldValidator) {}
should i not validate the params and validate the object after creation?
$oWorld = new World(....);
$oWorldValidator->validate($oWorld);
should i discard the constructor and use static methods to create valid world object?
private function __construct() // do not use
public function createValidWorld(....) {} // validate and exceptions here
how important is the first rule? (no exceptions in constructor method) can the object be responsibe for its validation ? is seperation of concerns realy the same as single responsibility?
thx for your time and help.