Reputation: 23680
I'm just implementing Exceptions in to my code for the first time.
For exceptions that are used within a class function, where is best to define the exception?
Here's a class and a few functions that represent a piece of code that I have written:
//
// Do I define my exception here?
//
class CreateQuizException extends Exception {}
class Quiz()
{
// Define the items in my quiz object
$quiz_id = null;
$quiz_name = null;
// Function to create a quiz record in the database
function CreateQuizInDatabase()
{
//
// OR DO I DEFINE IT WITHIN THE FUNCTION??
//
class CreateQuizException extends Exception {}
try
{
// Insert the record in to the database
$create_quiz = mysql_query("INSERT INTO quizzes (quiz_name) VALUES ("' . $this->quiz_name . '")");
if (!$create_quiz)
{
// There was an error creating record in the database
throw new CreateQuizException("Failed inserting record");
}
else
{
// Return true, the quiz was created successfully
return true;
}
}
catch (CreateQuizException $create_quiz_exception)
{
// There was an error creating the quiz in the database
return false;
}
catch (Exception $other_exception)
{
// There was another error
}
}
}
Is it:
Upvotes: 1
Views: 390
Reputation: 682
Use an autoloader function or library. I have been using this one:
http://static.invenzzia.org/docs/opl/3_0/book/en/autoloader.html
With very good results. Note that for the second option you must be using either namespaces, or the underscore naming convention:
class <vendor>_<package>_<namespace>_<class_name> {}
and saving the class files in the proper directory.
And then define Exceptions on individual files inside your namespace. Since Exceptions are classes, those will be autoloaded when needed.
May look as an overkill now, but it's better on the long term.
Upvotes: 1
Reputation: 59699
Typically I've seen and used two places for Exception placement for two distinct reasons:
Outside of the class definition, since is it possible that the Exception will be thrown to the caller and I want to be explicit that this Exception is in the public namespace. It might be useful to note that I really only see Exceptions placed at the end of the class file in which they're defined, but this is preference and has no functional significance.
class Foo {
}
class FooException extends Exception {}
Inside of the class definition, but outside of the method definitions, if the Exception is local to this class, not relevant outside of the class, and not going to be handled by the caller. Typically, the private
access modifier is introduced to enforce this.
class Foo {
public function bar() {
}
private class FooException extends Exception {}
}
However, I do not believe that PHP supports nested classes, as I cannot get one example of nested classes to parse. I am drawing my examples more from Java than PHP.
Placing Exception definitions inside methods seems too granular, you'll end up polluting your code with Exceptions and end up with:
class MyFunctionReturnedABadValueAndNowImSad extends Exception {}
Upvotes: 2