Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95039 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25273 invoked from network); 11 Aug 2016 14:49:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Aug 2016 14:49:39 -0000 Authentication-Results: pb1.pair.com header.from=lester@lsces.co.uk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=lester@lsces.co.uk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lsces.co.uk from 217.147.176.230 cause and error) X-PHP-List-Original-Sender: lester@lsces.co.uk X-Host-Fingerprint: 217.147.176.230 mail4-3.serversure.net Linux 2.6 Received: from [217.147.176.230] ([217.147.176.230:50139] helo=mail4.serversure.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 81/10-24432-1809CA75 for ; Thu, 11 Aug 2016 10:49:38 -0400 Received: (qmail 23386 invoked by uid 89); 11 Aug 2016 14:49:35 -0000 Received: by simscan 1.3.1 ppid: 23380, pid: 23383, t: 0.0806s scanners: attach: 1.3.1 clamav: 0.96/m:52/d:10677 Received: from unknown (HELO ?10.0.0.7?) (lester@rainbowdigitalmedia.org.uk@81.138.11.136) by mail4.serversure.net with ESMTPA; 11 Aug 2016 14:49:35 -0000 To: internals@lists.php.net References: <10fbcb03-5de8-4d9a-da1c-7e2bf77937cb@lsces.co.uk> <5657afc7-7569-5fc4-4a5a-27ed786c4fa5@gmail.com> Message-ID: <0825c173-5cb4-7f65-cf34-b45ca30919a3@lsces.co.uk> Date: Thu, 11 Aug 2016 15:49:34 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2 MIME-Version: 1.0 In-Reply-To: <5657afc7-7569-5fc4-4a5a-27ed786c4fa5@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Simple variable handling. From: lester@lsces.co.uk (Lester Caine) On 11/08/16 14:51, Rowan Collins wrote: > On 11/08/16 13:50, Niklas Keller wrote: >>> > If not by using exceptions, how would you handle them if you assign >>> such >>> > checks to variables and assign a wrong value? > > On 11/08/2016 14:29, Lester Caine wrote: >> if ( !$age->set( $result_set['age'] ) ) { // handle error } >> else { // next step } > > So, cutting out the commentary about styles of coding, the answer to > Niklas's question is "I would replace the assignment with a function or > method that returned success based on validation result"? > > To combine some of your examples: > > // Set up the constraints: > $age->setannot( 'number', range(1,120) ); > > // Later: > $valid = $age->set( 'not a number' ); > // or: > $age = $result_set['age']; > $valid = $age->is_valid(); > > > This would result in $valid being false, but $age presumably still being > the string 'not a number'? Or perhaps still being null, as though the > assignment hadn't happened? > > It seems like it would be more useful if the user didn't have to > remember to call is_valid() on each variable before using it. That means > having either the write operation, or subsequent read operations "fail" > in some way. > > $age_at_next_birthday = $age + 1; // reads from $age; is $age valid? > > Throwing exceptions is a way of making operations like this "fail", > rather than propogating the bad data to other parts of the program. > > Does this look like the kind of thing you were imagining? For an 'exception' model of PHP then I have no problem with actions throwing exceptions, but from a 'work flow' model then replacing the exceptions with checks simply works in my model. The question is not how you flag an error, but rather when do you check for one. If the 'load' function from a database record or the populate from a web form results in $age not being valid one handles that situation based on the data model. If you are propagating that data after validating has failed then the program flow is wrong and adding some exception when you use the duff data later does nothing to help? -- Lester Caine - G8HFL ----------------------------- Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk