PHP
  Home arrow PHP arrow Page 3 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

The Standard PHP Library, Part 1
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 40
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - The Exception Class Hierarchy
    ( Page 3 of 4 )

    As I noted before, the Exception object is essentially a data container from the developer's standpoint. The only overridable method is "__toString()", a magic function invoked by default when the class is used in string context. Below is the class definition for the built-in exception class.

    class Exception
    {
       protected $message = 'Unknown exception';  // exception message
       protected $code = 0;             // user defined exception code
       protected $file;                // source filename of exception
       protected $line;                    // source line of exception

       function __construct($message = null, $code = 0);

       final function getMessage();    // message of exception
       final function getCode();          // code of exception
       final function getFile();            // source filename
       final function getTrace();  // an array of the backtrace()
       final function getTraceAsString();   // formated string of trace

       /* Overrideable */
       function __toString();                      // formated string for display
    }

    The methods provided by the Exception class allow a developer to get all relevant information required to debug an error. The most useful feature is the stack trace, which was previously only available to developers using the Zend Debugger. The stack trace allows you to follow the code path that generated the exception from end to end, tracing back through calling methods, files and lines, until the source function call is displayed.

    In complex applications, this information is truly priceless and eliminates, to some degree, the ad hoc debugging methods typically employed in PHP to trace errors such as inserting "die()" or "print_r()" calls at key places in your code to get a trace. You will still have to do these things from time to time but overall, Exceptions should ease debugging pains considerably. Other methods allow you to retreive the message and code that were passed into the constructor, the stack trace in string format (instead of an array), and the file name and line number where the exception was thrown.

    Like most other clases, the base class Exception can and should be extended to fit its intended usage. The purpose of extending the Exception class is, almost exclusively, to allow instantiation of Exceptions with intuitive, specific names rather than the nonspecific (but still intuitive) Exception.

    Developers should be very careful in how they extend the Exception class, as the purpose of the class itself requires minimal functionality.If a task needs complex work to take place before an Exception can be built with the proper data, then you would be right to put that functionality into the Exception, but adding functionality for functionality's sake is not the right approach. For example, you would not create several methods to output an error message in various formats. There are already quite a few children of the Exception class in the SPL, all of which are made for use in other SPL classes or by the runtime itself.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek