PHP5: Errors & Exceptions

Posted on February 21, 2011

PHP5 adds a bit to its error model, plus adds a completely new model for exceptions.

Errors

PHP5 has a few new error codes. There’s E_STRICT, which will “suggest changes to your code which will ensure the best interoperability and forward compatibility of your code”. This is very useful in development, as you typically want to make your code as robust and future-proof as possible. E_STRICT is not included in E_ALL, so you need to explicitly include it in your code or the PHP configuration:

error_reporting(E_ALL | E_STRICT);

A similar constant that is included in E_ALL is E_DEPRECATED. I believe the difference is that E_STRICT offers lint-like suggestions to improve your code, while E_DEPRECATED will warn you if a PHP element you’re using has been marked as “deprecated” by the PHP developers. So, E_DEPRECATED is more important and included in E_ALL because your code actually won’t work in a future release. PHP5 also defines an E_USER_DEPRECATED constant that you can use in your own applications.

Also new is E_RECOVERABLE_ERROR, which I mentioned in my Type Hinting post, and only occurs for mismatched types as far as I can tell.

Finally, there is a slight change if you are using your own error handler as well as track_errors: You should return false from your error handler if you want $php_errormsg to be populated.

Exceptions

In the same vein as its addition of OOP elements, PHP5 adds exceptions like what you’d find in C++ or Java. I won’t go into how exceptions work, since PHP follows the same model as other languages, but I’ll write about some of the specifics when using PHP exceptions. Think Vitamin (among others) has a tutorial on PHP exceptions if you would like more of an intro to exceptions.

Here’s a short example of exceptions in action:

function inverse($x)
{
  if (!$x)
  {
    throw new Exception('Division by zero.');
  }
  else return 1/$x;
}

try
{
  echo inverse(5) . "n"; // displays "0.2"
  echo inverse(0) . "n"; // displays "Caught exception: Division by zero."
  echo "Hellon"; // never executes
}
catch (Exception $e)
{
  echo 'Caught exception: ',  $e->getMessage(), "n";
}

PHP itself does not really use exceptions — it still uses the old error model except for some OO extensions — but you can use them for your own applications. Just use or extend the Exception class — the standard library has some pre-made ones you can use. Here’s an example of a custom exception:

class MyException extends Exception
{
  // Redefine the exception so message isn't optional
  public function __construct($message, $code = 0, Exception $previous = null)
  {
    // some code

    // make sure everything is assigned properly
    parent::__construct($message, $code, $previous);
  }

  // custom string representation of object
  public function __toString()
  {
    return __CLASS__ . ": [{$this->code}]: {$this->message}n";
  }

  public function customFunction()
  {
    echo "A custom function for this type of exceptionn";
  }
}

PHP is a little different from other languages in that you can also define an exception handler for any exceptions that aren’t caught by your application:

function exception_handler($exception)
{
  echo "Uncaught exception: " , $exception->getMessage(), "n";
}
set_exception_handler('exception_handler');

throw new Exception('Uncaught Exception');
echo "Not Executedn";

I would recommend using exceptions instead of error checking for your own code. (Of course, each situation should be evaluated individually.) The pre-made SPL exceptions should cover most situations, but of course you can create your own by extending the Exception class. You can also easily translate errors into exceptions with the ErrorException class.

Leave a Reply

  1.  

    |