Home arrow PHP arrow Page 2 - The Standard PHP Library, Part 1

Simple Example - PHP

While most of the excitement surrounding the release of PHP 5 focused on its XML and object-oriented features, the Standard PHP Library (SPL) also saw some significant improvements that went mostly unnoticed. In the first of two articles covering the SPL, David Fells discusses the Exception class, which lets programs handle errors more gracefully and simplifies debugging.

TABLE OF CONTENTS:
  1. The Standard PHP Library, Part 1
  2. Simple Example
  3. The Exception Class Hierarchy
  4. A Simple Exception Extension
By: David Fells
Rating: starstarstarstarstar / 42
December 07, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now that we have covered the basics of what an exception is, we will look at a code snippet to see them in action.

class Math {
  function divide($divisor, $dividend = 1) {
    if (!is_numeric($divisor))
    throw(new Exception('Divisor is not a number'));
    if (!is_numeric($dividend))
    throw(new Exception('Dividend is not a number'));
    if ($dividend == 0)
    throw(new Exception('Division by zero'));
   
    return ($divisor / $dividend);
  }
}

try {
  print Math::divide(1, 0);
}
catch(Exception $e) {
  print $e;
}

If you run this code sample, you will see a dump of the contents of the exception thrown by our divide function. The object contains a lot of useful info like the file name and line where the error occurred and the trace. If you are logging errors or displaying them to the screen (for debugging), this information is priceless. Let's go over the important parts of this code.

if (!is_numeric($divisor))
  throw(new Exception('Divisor is not a number'));
if (!is_numeric($dividend))
  throw(new Exception('Dividend is not a number'));
if ($dividend == 0)
  throw(new Exception('Division by zero'));

This piece of code checks to see if the divisor and dividend are numbers and if the dividend is a non-zero value. If any of these checks fail, our method calls the new function "throw()", passing it a new Exception object. We send our message to the constructor of Exception so the debugging information is descriptive enough to be of use and to indicate the specific type of failure encountered by the function. Exceptions can also be extended to give them meaningful names, such as DivisionByZeroException for our case, but in general supplying a suitably descriptive message is adequate.

try {
  print Math::divide(1, 0);
}

Here we attempt to call "divide()". Placing it in a "try" control structure allows us to logically separate our code blocks, as not all operations require exception handling. We could generate other errors by making calls to divide with non-numeric input.

catch(Exception $e) {
  print $e;
}

This is where the exception is actually caught for handling. We could do something with this error, such as tell the user they cannot divide by zero with the "divide()" function, but in this case we are simply invoking the "$e->__toString()" magic method by treating "$e" as a string - ultimately yeilding an informative error message for the developer.



 
 
>>> More PHP Articles          >>> More By David Fells
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: