Welcome Guest, Not a member yet? Register   Sign In
Multiple Exception Sub Classes With CodeIgniter
#1

[eluser]Unknown[/eluser]
Hi guys,
Long time user of CodeIgniter, first time poster.

I'm starting the build of a new small application and I'm trying to implement more exceptions and exception handling into my code where they should be.

My goal is to have a range of exception sub classes that I can throw and handle in various scenarios. They will be fairly descriptively named.

In a standard php app you can imagine my goal could be achieved by having a file included with a few examples like below:
Code:
class PatientIDNotExistsException extends Exception {}
class PatientFailedToSyncException extends Exception {}
class WebServiceAuthFailedException extends Exception {}

My question is how do I go about this in CodeIgniter?

I've been reading the docs (http://ellislab.com/codeigniter/user-gui...asses.html) and I don't want to extend the CI_Exceptions class with a single My_Exceptions class, because I need multiple.

I also read this post from someone who was wanting what I'm after, but there was no resolution http://ellislab.com/forums/viewthread/67096/

The guy that came the closest was http://ellislab.com/forums/viewreply/952628/ but that still doesn't seem like the nicest way of doing things.

Is there a better way of handling this? Short of adding a seperate exception library class and including it with the autoloader?

Thanks guys
#2

[eluser]Aken[/eluser]
Various options:

1) Include them in /application/config/constants.php
2) Extend the Exceptions class by creating a file at /application/core/MY_Exceptions.php, and define them in that file along with the extended Exceptions class (which wouldn't have anything in it unless you wanted to add something specific).
3) Add them to index.php, or have index.php include/require a file that has them defined.

There isn't a specific use case for this that is "standard CI way". But CI is just PHP, so use native PHP in whatever way is convenient for you.
#3

[eluser]Unknown[/eluser]
[quote author="Aken" date="1347322012"]Various options:

1) Include them in /application/config/constants.php
2) Extend the Exceptions class by creating a file at /application/core/MY_Exceptions.php, and define them in that file along with the extended Exceptions class (which wouldn't have anything in it unless you wanted to add something specific).
3) Add them to index.php, or have index.php include/require a file that has them defined.

There isn't a specific use case for this that is "standard CI way". But CI is just PHP, so use native PHP in whatever way is convenient for you.[/quote]
Option 2 is similar to a solution in the post I mentioned. I think I'll just use that then.

Thanks Aken.




Theme © iAndrew 2016 - Forum software by © MyBB