Home arrow PHP arrow Sub Classing Exceptions in PHP 5

Sub Classing Exceptions in PHP 5

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.

TABLE OF CONTENTS:
  1. Sub Classing Exceptions in PHP 5
  2. Getting started with exceptions with PHP 5
  3. Catching MySQL-related exceptions with the PHP 5 Exception class
  4. Modifying the MySQL class's signature to trigger customized exceptions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
October 15, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: