Properly storing error messages and displaying them with OOP
The big disadvantage of putting the validation to the setter is that you are never able to set invalid values. In most cases this is fine, but in some scenarios a valid user input differs from a more general valid field content. In addition the validation is also executed to internal setter uses where know that they are valid.

That said, I recommend setters to be only setters and not more.

The validation itself can be placed in a separate method or even in a separate class (depending on the complexity).

If you only want to display one error message, throwing an exception within the validation should be fine.

If you want to collect multiple error messages, you might want to build a generic validation class:

$valid=new Validation();
$valid-&gt;assertNotEmpty($name,"name","Insert a name please");
$valid-&gt;assertLength($name,3,50,"name","...");
...
if ($valid-&gt;hasErrors()) ...


You could extend this to a CourseValidation if you need it at multiple locations.

$valid=new CourseValidation($code,$name);
...