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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to