views:

453

answers:

1

Hi everyone,

We're working with a new codeigniter based application that are cross referencing different PHP functions forwards and backwards from various libraries, models and such.

We're running PHP5 on the server and we try to find a good way for managing errors and status reports that arises from the usage of our functions. While using return in functions, the execution is ended, so nothing more can be sent back. Right?

What's the best practice to send a status information or error code upon ending execution of actual function?

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

+1  A: 

Should we look into using exceptions or any other approach?

Since you are committed to PHP5 I think using exceptions in your custom libraries is a great idea. The use of Try/Catch and Exceptions are going to provide a lot better control of your code as well as potentially more robust error logging.

Extending the Exceptions class is going to allow you to have a granular level of control over what kind of exceptions your models and libraries can throw and how you get to react to them.

I can imagine having a model throw an dataInputException if it was provided bad input, and a dataOperationException if it failed to operate on the data (say $this->db->insert() returned a failure). How you handle each kind of exception might be different.

In terms of error reporting for development, the Exceptions class methods are going to provide you a lot of information (like the file name and line number).

Also, some third-party code makes use of Exceptions (such as Facebook Connect's PHP Lib).

Natebot
Thanks Natebot. We'll look more into exceptions and see what we can learn from using it. It seems like it's made for what we're doing. Are there any other good options to it?
Industrial
Hi again! Do you have some experience from doing this in codeigniter? If so, how did you extend the exceptions class?Thanks!
Industrial
I have limited experience, mostly having models throw exceptions when anything in the model method fails. The Controller method try/catches the model method call -- because a failure of the model method often means the controller needs to do something radically outside of what it expects (such as redirect to an error msg). The catch can log the error (or send an email to my monitoring service).
Natebot