Home arrow PHP arrow Page 3 - A Custom Exception Class for Dynamic Twitter Signature Images with PHP

Building a custom exception interface - PHP

Welcome to part four of a five-part series on creating a dynamic Twitter signature image in PHP. In the last segment, I showed you how to implement PHP 5 exceptions as an error-handling mechanism in your signature image application. Today we’re going to expand upon that concept by creating a custom exception class for handling error states in our application.

TABLE OF CONTENTS:
  1. A Custom Exception Class for Dynamic Twitter Signature Images with PHP
  2. Examining the SignatureImage class
  3. Building a custom exception interface
  4. Building the custom exception
  5. Implementing the custom exception
By: Nilpo
Rating: starstarstarstarstar / 3
August 27, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To begin piecing together a custom exception, you’ll first need to understand how the native Exception class is constructed.  For that you’ll need to sit down and examine the PHP documentation, so I’ll save you a little time.

interface ExceptionInterface

{

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

   

    public function getMessage();        // message of exception

    public function getCode();           // code of exception

    public function getFile();           // source filename

    public function getLine();           // source line

    public function getTrace();          // an array of the backtrace()

    public function getTraceAsString();  // formatted string of trace

   

    public function __toString();        // formatted string for display

}

In the code above I’ve created an interface that lists all of the methods provided by the native Exception class.  By implementing this interface in our own custom exception class, we’ll have a framework to ensure that our own exception is constructed in the same manner as the original. 

In larger applications, this allows multiple developers to implement any custom exception without the need for additional documentation.  That’s an important time-saver any time you have more than one developer collaborating on a project.

Now that we’ve got an interface to define our custom exceptions, we can begin putting the code together to make it work.



 
 
>>> More PHP Articles          >>> More By Nilpo
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


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

Dev Shed Tutorial Topics: