Computerworld
PHP: Making an exception
Gavin Sherry (PC World)  24 January, 2005 07:04

Gavin Sherry continues his exploration of what makes PHP 5 so special. This month it's exceptional.

In the time between this and my last column, PHP 5 has moved out of beta and into stable release, which means that you can now begin using PHP 5 in production with the confidence that it's supported and well tested. This month, I'll be looking at one of the very exciting new features in PHP 5 - exceptions.

What are exceptions?

An exception is an error that occurs during the course of the execution of a script. When an error occurs in PHP 4, control is passed to the error handler, which outputs an error message to the user. A few months ago we looked at how to customise this error handling with set_error_handler(). The problem with this system is that you often need to code your scripts in such a way that you test for errors at each possible occurrence, swap error handlers around to match the code context you are in and more. This can be a nightmare, particularly if your application is being developed by more than one person, since you need to spend a lot of time getting the error handling right.

Object orientated languages such as Java, however, support an in-line mechanism which allows you to handle exceptions in a much more systematic way. This mechanism is to surround whole code blocks with an exception handler which is able to deal with any error in that block or, alternatively, pass it on to another exception handler. The way this is done can be seen in here:


01 try

02 {

03 /* code here */

04 }

05 catch(Exception $e)

06 {

07 /* recovery code */

08 }

Line 1 uses the try construct to instruct PHP that it should trap exceptions resulting from the execution of the code block which follows. If an exception occurs, control is passed to the catch block. This block is intended to allow the programmer to provide error recovery. If the catch block cannot resolve the error, it has the option of 're-throwing' the error. Consider the example below:


01 function test()

02 {

03 try

04 {

05 /* code */

06 }

07 catch(exception $e)

08 {

09 throw $e;

10 }

11 }

12 try

13 {

14 test();

15 }

16 catch(Exception $e)

17 {

18 /* recover here */

19 }

In this case, we have a function test(). The function tries to execute some code and if an exception occurs, control is passed to the catch block at line 07. We pretend that this catch block is unable to handle the error and we re-throw the error on line 09 using the 'throw' construct. We pass to this the exception itself: $e. By wrapping the call to test(), on line 14, in an exception block, we are able to catch the re-thrown error on line 16 and deal with it there.

Extending exceptions

In PHP 5, we can also extend exceptions. When dealing with a complex object-oriented project, you will want to be able to handle class specific exceptions. One of the most common places where this occurs is in dealing with databases, since errors encountered with the database are often completely unrelated to those in PHP - see here:


01 class SQLException extends Exception

02 {

03 var $state;

04 function _ _construct($SQLState)

05 {

06 $state = $SQLState;

07 }

08 function state2msg()

09 }

10 /* map SQLState code to message */

11 }

12 }

13 function runsql($sql)

14 {

15 /* prepare and send SQL query */

16 if($sqlstate)

17 throw new SQLException($sqlstate);

18 }

19 try

20 {

21 /* code, including runsql() */

22 }

23 catch(SQLException $e)

24 {

25 ech $e->state2msg();

26 }

27 catch($e)

28 {

29 /* Catch all axceptions */

30 }

We now have a very sophisticated exception handling frame work. On line 01, we declare a class SQLException which extends the Exception class which is built into PHP 5. We declare on line 04 a constructor which saves the SQLState passed in at object creation time. We declare on line 08 a function which will map the numeric error code sent from our database into a user friendly message. On line 13 we create a function, runsql(), which can execute an SQL query on a database server. If it encounters an error on line 16, we throw an exception (line 17) being careful to pass in the SQL error code, $sqlstate.

In our main code body, starting at line 19, we run some code, including runsql(), in a try block. If an ex­-ception is generated by runsql(), or some other function which is able to throw an exception of type SQLException, then we catch it at line 23. We then call the state2msg() method of SQLException and tell the user the error we've encountered. We're also careful on line 27 to catch any other exceptions which are not of the type SQLException.

The Evolution of PHP

PHP was originally created by Rasmus Lerdorf in 1995. The project started as a set of tools to measure traffic to his resume. Rasmus continued to add functionality to these scripts and began implementing it in C. One of the most important aspects of this first implementation, PHP/FI, was its ability to interpret data submitted by forms.

With significant assistance from Andi Gutmans and Zeev Suraski, PHP/FI was transformed into PHP 3.0. This release was marked by real programming language features, a large extension framework, high performance and great documentation. Developers worldwide took notice and installations grew exponentially.

With PHP 3.0 released, Andi Gutmans and Zeev Suraski returned to the core of PHP - the Zend engine. Meanwhile, many other developers began contributing more and more extensions to PHP. The significant number of contributions led to problems however: different aspects of PHP were at different leaves of maturity and releases were made with experimental code included, since no one was able to test some extensions due to their obscurity and niche appeal.

PHP 5.0 addresses the shortcomings of the interpreter by building a robust object-oriented base with all the features of other languages, while integrating XML and other functionality which was previously unstable and inconsistent. The future is certainly bright for PHP and its users.

www.php.net

www.php.net

The home of PHP, and where you'll find more details on the new features to be found in PHP 5.0. The front page deals with major news and events, while you can find the complete source code for the latest version of PHP in the downloads section, with accompanying manuals also available in the documentation pages.

As you'd expect, there's a wealth of information available here, from upcoming user group events and training centres, to FAQs and tutorials. If you're new to PHP, try the Getting Started guide first - you'll find it at http://au3.php.net/manual/en/getting-started.php.

Computerworld Buyer's Guide - Vendors Matched to this Article

Comments

Post new comment

Login or register to link comments to your user profile, or you may also post a comment without being logged in.
The content of this field is kept private and will not be shown publicly.
Enter the fully qualified URL, eg. http://www.example.com/
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Add to Google
Computerworld Buyer's Guide - Vendors Matched to this Article
Zones
Zone logoZones provide focussed content from Computerworld and leading technology partners.
Newsletter Subscription
Newsletter Subscription
Sign up for our Computerworld newsletters!
Syndicate content
 

Computerworld Webinar

Thursday, June 11th, 2009
10:30am EST (Sydney, Australia)
Screening at your PC

Computerworld is hosting a 30 minute live webinar to help you to learn how unified communications can save you money, foster innovation and business agility by making it easier for people to find, reach and collaborate with one another.

Register Now

Computerworld Community Comments
Whitepaper

Business Processes and Customers - Difficult Domains to Integrate

Get more out of CRM, integrate BPM with customer needs. This BPM Focus whitepaper discusses the problems with traditional CRM and explains the best practice scenarios for better customer interaction.

Enterprise IT Buyer's Guide
Find Technology Vendors Fast
 
Find vendors by name | Find by category
Sponsored Links
 
Send Us E-mail | Privacy Policy
Features List | Media Kit | Advertising | Contact Us

Copyright 2009 IDG Communications. ABN 14 001 592 650. All rights reserved.
Reproduction in whole or in part in any form or medium without express written permission of IDG Communications is prohibited.