Read up on the latest ideas and technologies from companies that sell hardware, software and services. Wireless LANs: Is my enterprise at risk?
Email Archiving Implementation: Five Costly Mistakes to Avoid
Solve Exchange Mailbox Storage Issues Once and for All
Enterprise Wireless WLAN Security
Why Security SaaS Makes Sense Today
Vendor Influence Curves And How You Can Get The Best Value Out Of Your Network
Solve Exchange Storage Problems Once and For All: A New Approach without Stubs or Links
Strategies for Eliminating .PST Files
Zones provide focussed content from Computerworld and leading technology partners.Newsletter Subscription
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:
|
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:
|
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:
|
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 PHPPHP 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.netThe 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 Member Login
Discover how SOA can create smarter outcomes for your business.
Attend and learn:
- How SOA is helping leading companies to become more agile
- Where you should be applying SOA processes in your company
- The top SOA implementation mistakes to avoid
Click here for more information.
- +
Computerworld Live Podcast #97: The Future of Enterprise Networking 25/07/2008 09:45:36
This week CW Live chats with Mark Thompson, global sales and marketing manager for HP ProCurve, on the future of the enterprise networking. Mark discusses the trends we can expect to see in the near future and how the right infrastructure can ensure your enterprise network is secure. - +
Computerworld Live Podcast #96: Security at the Edge 11/06/2008 09:22:22
CW Live speaks with Amol Mitra, HP ProCurve Director of Marketing for Asia Pacific and Japan. Today's topic: how enterprises are starting to shift away from simply controlling security via server logins, firewalls and moving to more adaptive security frameworks. - +
Data Management Edition #10: Multi-Petascale Systems 02/05/2008 09:12:33
This week we look at sustainability and the development of multicore technologies to build multi-petascale systems. - +
IT Security Edition #11: How to poison the Storm botnet 01/05/2008 08:51:55
This week CW Live presents a case study on how to poison the notorious Storm botnet . Plus we take a look at Cisco's plans for Ironport. - +
IT Security Edition #10: Cyber-battles fought and won 24/04/2008 11:09:47
Vendors bow to end user pressure to improve product security, and we take a look at the latest concepts shaping the cyber-battlefield of the future.
NetStar Networks Calls Brisbane Home 2008-10-13 12:01:00+10
New Verizon Business Managed Service Makes Collaboration Easier 2008-10-13 10:06:00+10
F-Secure achieves excellent results in Internet security suite comparison 2008-10-10 14:37:00+10
M2M Connectivity announces the new Sierra Wireless MC8792V embedded module for 900 MHz 3G/HSPA networks 2008-10-10 08:51:00+10
Pitney Bowes MapInfo Launches New Version of AnySite 2008-10-10 05:58:00+10
Solve Exchange Storage Problems Once and For All: A New Approach without Stubs or Links
The management of Microsoft® Exchange storage growth is the most challenging problem facing Exchange administrators. Because of the popularity of email as a communication technology, and because users tend to keep email, maintaining adequate storage on the Exchange Server is a constant challenge. Learn how to maintain the space you need by reading on.










