Imagine that I have a method like User::validate($data), and mostly the developer expects only that is returns a boolean (true mean "ok, validated with success"). But, in some specific cases, he need check why validation failed. What is the best way to check that?
I thought in following ways:
- Use an argument like
User::validate($data, $throwsException = false)that whentrue, will throws an exception if it fail -- instead of returnsfalse; - Always throws an exception. So the developer need care about in catch exceptions of this method -- I personally don't like that;
- Instead of returns a boolean, returns a string or const (like
FAIL_INVALID_NAME), but usestrueas success -- or another const, likeSUCCESS; - Create an additional method like
User::validateWithException($data)that, if it fails, throws an exception instead of returnfalse; - Create an additional method like
User::validateReason($data)that will returns a string with the validation result -- likefail:invalid-nameorsuccess; - Create an additional method like
User::getLastError()that will returns a string or const with last error ocurred on classUser, like after the validation method;
""if you want. – Jul 13 '15 at 12:54if($user->validate()->isSuccess()), right? – David Rodrigues Jul 13 '15 at 12:56