Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95110 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25794 invoked from network); 13 Aug 2016 09:01:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Aug 2016 09:01:04 -0000 X-Host-Fingerprint: 80.177.120.119 marston-home.demon.co.uk Received: from [80.177.120.119] ([80.177.120.119:14637] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E1/B7-55605-EC1EEA75 for ; Sat, 13 Aug 2016 05:01:04 -0400 Message-ID: To: internals@lists.php.net References: <10fbcb03-5de8-4d9a-da1c-7e2bf77937cb@lsces.co.uk> In-Reply-To: Date: Sat, 13 Aug 2016 10:00:52 +0100 Lines: 1 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 16.4.3564.1216 X-MimeOLE: Produced By Microsoft MimeOLE V16.4.3564.1216 X-Posted-By: 80.177.120.119 Subject: Re: [PHP-DEV] Simple variable handling. From: TonyMarston@hotmail.com ("Tony Marston") "Niklas Keller" wrote in message news:CANUQDCjaXeJsCAjyE+q-UhXCFjexwRYWLKzMfwRQVDaEwVbLog@mail.gmail.com... > >2016-08-11 14:42 GMT+02:00 Lester Caine : > >> 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