Best practice for error handling in codeigniter / php applications

We are working with a new application based on codeigniter, which cross-references various functions of PHP back and forth from different libraries, models, etc.

We are running PHP5 on the server, and we are trying to find a good way to manage errors and status reports that arise from using our functions. When using return in functions, execution ends, so nothing else can be sent back. Right?

What is the best practice for sending status information or an error code after the actual function completes?

Should we study using exceptions or any other approach? http://us.php.net/manual/en/language.exceptions.php

+4
source share
1 answer

Should we look for exceptions or any other approach?

Since you are committed to PHP5, I think using exceptions in your user libraries is a great idea. Using Try / Catch and Exceptions will provide much better control over your code, as well as potentially more reliable error logging.

Extending the Exclusions class allows you to have a granular level of control over which exceptions your models and libraries can display and how you react to them.

I can imagine that the model threw a dataInputException if it was given a bad input, and a dataOperationException if it could not work with the data (for example, $ this-> db-> insert () returned a failure). How you deal with each type of exception may be different.

As for development bug reports, the Exceptions class methods are going to provide you with a lot of information (like file name and line number).

In addition, some third-party codes use Exceptions (for example, Facebook Connect PHP Lib).

+5
source

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


All Articles