PHP error handling

We are building a PHP application based on the good old codeigniter framework, and we have run into problems with a massive chained action consisting of several model calls that together are part of a large transaction in a database.

We want to be able to carry out a list of actions and receive a report on the status of each of the functions, regardless of the result.

Our first initial idea was to use PHP5 exceptions, but since we also need status messages that do not violate script execution, this was our solution that we encountered.

This is a bit 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 is as follows:

 $var = $this->products->addProductGroup($array); if (is_array($var)) { foreach ($var as $error) { echo $error . '<br />'; } } 

This is apparently a very fragile solution to do what we need, but it is not scalable, not efficient compared to pure PHP exceptions, for example.

Is this really how this stuff is usually handled in MVC-based applications?

Thanks!

UPDATE: We did a fair share of the search and found this PHP function: register_shutdown_function. Is this what we are looking for? I have no idea and I can’t make it work the way we want it ... Link: http://php.net/manual/de/function.register-shutdown-function.php

+4
source share
1 answer

You can set your own exception handler in PHP - http://php.net/manual/en/function.set-exception-handler.php

This will allow the script to continue execution. You can create a function that registers status in the database. Another function and / or class can retrieve them to update the current status report. If you throw a custom exeption ie.throw StatusException, your custom handler can pick up the type of exception and properly register it when performing the default actions for other standard exceptions ...

Alternatively, using a database, you can use an exception handler to store information in a session object, etc., which will be specified later only for the current session.

If using an exception handler is the right thing, I'm still not sure, because I'm still a bit vague about what you are trying to do, but my method will allow the code to continue executing when handling the exceptions.

+2
source

Source: https://habr.com/ru/post/1308291/


All Articles