Error handling in PHP
- by Industrial
Hi guys,
We're building a PHP app based on Good old MVC (codeigniter framework) and have run into trouble with a massive chained action that consists of multiple model calls, that together is a part of a big transaction to the database.
We want to be able to do a list of actions and get a status report of each one back from the function, whatever the outcome is.
Our first initial idea was to utilize the exceptions of PHP5, but since we want to also need status messages that doesnt break the execution of the script, this was our solution that we came up with.
It goes a little something like this:
$sku = $this->addSku( $name );
if ($sku === false) {
$status[] = 'Something gone terrible wrong';
$this->db->trans_rollback();
return $status;
}
$image= $this->addImage( $filename);
if ($image=== false) {
$error[] = 'Image could not be uploaded, check filesize';
$this->db->trans_rollback();
return $status;
}
Our controller looks like this:
$var = $this->products->addProductGroup($array);
if (is_array($var)) {
foreach ($var as $error) {
echo $error . '<br />';
}
}
It appears to be a very fragile solution to do what we need, but it's neither scalable, neither effective when compared to pure PHP exceptions for instance.
Is this really the way that this kind of stuff generally is handled in MVC based apps?
Thanks!