oop - PHP OO - How should I handle soft errors in multiple classes? -
i apologise if has been answered somewhere, haven't managed find answer far - maybe i'm searching wrong thing!
i trying figure out how handle errors in oo php system, used generate web pages. example explain mean.
imagine have content class, form class , formobject class, hold information on page content, web forms , form fields. classes can run multiple mysql queries via db class. users can create new content or forms in back-end. when this, use classes create , store data in database.
i have system class, used generate web pages. system class checks should displayed on front-end, builds appropriate content , form objects, generates html , outputs screen.
i have checks serious errors, stop code going further. however, problem want feed "soft errors" front-end. example, maybe system class builds form object, in-turn builds fields using formobject class. formobject class queries database field name, field name not found. db class returns error. want able feed message front-end says field name has not been found.
what best way "soft error" message system class, can outputted front-end? realise simple in particular example, more classes added and, crucially, more levels added, problem becomes bit bigger.
one way thought of doing have error class. system create error object , pass on each content , form object created. form class pass same error object formitem class. whenever error found, logged via method in error class. system can access original error object , output errors. however, system grows, more classes added, , more objects created, quite confusing. there better way?
you might want use either
- something global classes can access (e.g. global variable or singleton), or
- something passed in instantiations of classses producing call 'soft errors'
to collect such errors. want use whatever collected , add output in system
class somehow.
to more specific...
this example solution using global:
global $softerrormessages = array(); class { function samplefunctiona() { // [...] // code setting $result valid value // or false if error occured if($result === false) // check validity { global $softerrormessages; $softerrormessages[] = "the sample function caused soft error"; return; } // [...] // code requiring valid $result } }
if use such global, can access system
class , put contents right places of output.
however, if perform unit tests, might not want use globals or global-like solutions (like singletons). here example 'error collection' approach:
class errorcollector { private $errors = array(); function adderror($error) { $this->errors[] = $error; } function geterrors() { return $this->errors; } } class { private $errorcollector; function __construct(/* additional parameters */, errorcollector $errorcollector) { // [...] // additional instantiation stuff $this->errorcollector = $errorcollector; } function samplefunctiona() { // [...] // code setting $result valid value // or false if error occured if($result === false) // check validity { $this->errorcollector->adderror("the sample function caused soft error"); return; } // [...] // code requiring valid $result } }
you instantiate errorcollector
once , pass other class instantiations. let objects perform duties (and possibly add soft errors errorcollector
). once they're done, system
class error messages , - again - place them @ right place of output.
Comments
Post a Comment