PHP
  Home arrow PHP arrow Sub Classing Exceptions in PHP 5
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

Sub Classing Exceptions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-10-15


    Table of Contents:
  • Sub Classing Exceptions in PHP 5
  • Getting started with exceptions with PHP 5
  • Catching MySQL-related exceptions with the PHP 5 Exception class
  • Modifying the MySQL class's signature to trigger customized exceptions

  • 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


    Sub Classing Exceptions in PHP 5
    (Page 1 of 4 )

    If you do any serious programming, whether it's in PHP 5 or some other language, you've needed to know how to handle run time errors and other "exceptional" conditions. You can do this by making your program throw generic exceptions. Or you can unlock the potential of PHP 5 and learn how to create custom exceptions, which is the subject of this four-part series.

    Introduction

    As you may know, many mature object-oriented programming languages, like Java and C++, let developers work with exceptions in a fairly simple fashion. Of course, in these specific cases, this feature has been available for a long time, and it’s been a generous source of inspiration for younger languages, including PHP 5.

    While PHP 5's exception mechanism is pretty solid and can be mastered by inexperienced programmers in a short time, its full potential is rarely exploited when developing web applications. What’s more, if you’re anything like me, then it’s probable that in the past few years you’ve built some scripts that handled all run time errors and “exceptional” conditions by means of abrupt “die()” statements.

    Yes, I have to admit that I’m guilty of that mistake too. I’m trying hard not to do it anymore, since I learned how to work with exceptions. Now, speaking more seriously, I’m sure that you already know how to wrap your PHP 5 scripts into neat “try-catch()” blocks, and of course, all of your classes handle certain types of errors by means of clean exceptions. That sounds fine.

    However, let me ask you the following question: how far did you go when using PHP 5’s exception mechanism? If your answer is simply: "hey, generic exceptions are all that I need for my web applications," then this series of articles might be useful to you. In it, you’ll learn how to create subclasses from the built-in “Exception” class bundled with PHP 5, in this way providing your programs with the ability to handle customized exceptions.

    Now that you’re aware of the goal of this series, it’s time show some action, so jump ahead and start learning how to use inheritance to throw and handle specific exceptions in PHP 5. It’s going to be a fun experience!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing the Iterator SPL Interface
    - Building a Data Access Layer for the Data Ma...
    - Building a Singleton Database with Restricti...
    - Working with Reflected Properties with the R...
    - The Iterator, Countable and ArrayAccess SPL ...
    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer
    - Using Different Paths for Attachments with S...


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code

     



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