PHP
  Home arrow PHP arrow The Standard PHP Library, Part 1
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Standard PHP Library, Part 1
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 38
    2004-12-07

    Table of Contents:
  • The Standard PHP Library, Part 1
  • Simple Example
  • The Exception Class Hierarchy
  • A Simple Exception Extension

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Standard PHP Library, Part 1


    (Page 1 of 4 )

    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.

    Introduction

    Amid all the noise and excitement surrounding the release of PHP and its shiny new XML and object oriented features, the Standard PHP Library (SPL) slipped by without much recognition. Exception handling has been a major feature add for PHP 5, and the classes built into PHP are a part of the SPL. This article will demonstrate basic application of several SPL classes and provide information on the classes and interfaces available to PHP 5 programmers.

    This article is the first part in a series discussing the classes available in the SPL and their uses. In this article we will discuss the Exception class and its application. Exceptions serve a dual purpose - they allow applications to gracefully handle errors, and they allow simplification of the debugging process, which adds up to a more efficient development process and less stress for developers.

    Exceptions

    Exceptions have been around for quite a while in languages like C++ and Java, but simple scripting languages have never had support for this graceful error handling device. That changed with the release of PHP 5. Exceptions are not all that special as far as objects go; they are essentially containers for error information. The interesting part is how exceptions are created and identifed. To use exceptions, one only needs to be familiar with the special control structures and functions associated with them. They are built-in and require no configuration and only a handful of brain cells. Exception handling requires proper use of "try"/"catch" blocks and the use of the "throw()" function.

    Exceptions are created, or thrown, in one of two ways. Either a developer tells the application to throw an exception when a specific error condition is met (such as data out of the expected range as a function parameter) or the language itself throws an exception when it encounters an application error. The latter type are generated from a lower level in terms of system architecture, but the end result is the same: developers can catch exceptions that are thrown by application code and either ignore them, attempt to recover or gracefully fail. Any option is better than creating a web of error handling methods (such as the PEAR Error class) or by using "die()", but exceptions are, pun intended, an exceptional solution to the problem of graceful, clean error handling.

    More PHP Articles
    More By David Fells


       · I think this will be very helpfull. Extremely good article
       · This is really a great article. Another good resource is George Schlossnagle's...
       · You had a problem downloading the pdf? What version of acrobat reader do you have...
       · When trying to access the PDF file, I get a blank pop-up window. I have Adobe...
       · I very much enjoyed this informative article, but one thing stood out as a bit off. ...
       · Thanx, but there are a few typo'sPage 4 of 4 top of the page:print divide(1,...
       · blame the typos on devshed =)as for the exceptions, i didn't intend to...
       · ok, i've turned all the error message on.here's the code:try{ print...
       · You have to throw errors in the try{} section if the unction / class doesn't throw...
       · you also have to use $e->getMessage() to get the exception string...unless you want...
     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT