"Niklas Keller" wrote in message
news:canuqdcjaxejscajye+q-uhxcfjexwrywlkzmfwrqvdaewvb...@mail.gmail.com...
2016-08-11 14:42 GMT+02:00 Lester Caine <les...@lsces.co.uk>:
On 11/08/16 04:56, Michal Brzuchalski wrote:
> You want to stick such validation at runtime at any time with variable
and
> throwing \TypeError at any time constraint is broken - wouldn't it
> cause
of
> throwing much more unexpected exceptions during runtime?
> Imagine you'll be passing such variable with constraint into some
> object
> who operates on it and it should expect \TypeError at any time because
you
> newer know what sort of constraint and optional validation callback is
> sticked to variable!
Now this is where the fundamental difference in styles comes in.
PERSONALLY I would not be looking to throw exceptions at all. The whole
point of validation is to handle any validation error ... and it is an
error not an exception.
If not by using exceptions, how would you handle them if you assign such
checks to variables and assign a wrong value?
Regards, Niklas
I agree 100% with Lester. Data validation errors are NOT exceptions, they
are simple errors. Exceptions are supposed to be for exceptional or
unexpected events, usually pointing to a bug in the code which needs fixing,
whereas data validation errors are totally expected and regular occurrences.
I solved the data validation problem over a decade ago by writing a simple
validation function that has two input variables - an associative array of
field names and their values (such as the $_POST array), and a multi-level
array of field specifications which has a separate array of specifications
(type, size, etc) for each field in that table. The output is an array of
errors which can contain zero or more entries. I DO NOT use exceptions
because (a) they are errors and not exceptions, and (b) if I throw an
exception on the first error then further validation is stopped, and
everyone knows that there may be multiple errors in a single array of
values.
If you are still writing code to perform such data validation then you are
behind the times. If you expect the language to perform the validation for
you then your expectations are unreasonable.
--
Tony Marston
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php