Monday, October 27, 2008

The elePHPants are coming

Today I received a package from Nexen, containing 50 elePHPants ! Whohoo !!!

There's so much fun unpacking them, I wanted to share it with you.

Unpacking elePHPants  50 elePHPants wrapped in a box Walking tall Army of elePHPants Follow the leader Rumble at the end
Blogged with the Flock Browser

Thursday, October 16, 2008

BugHuntDay or fixing bugs for fun

BugHuntDay.org is a joined effort between the Dutch PHP user group phpGG and the Belgian PHP community PHPBelgium to show that fixing bugs can be fun and is also a much appreciated way to contribute back to the global PHP community.

Fixing bugs is mostly considered the least favorite part of open-source projects, which often delays a good project growing into a great one. And this is a pity, because fixing bugs can really be fun and is very educative.

Since this BugHuntDay is not really just one day, but a series of days that both phpGG and PHPBelgium devote on fixing bugs of a PHP library or framework. A typical BugHuntDay is started with a small presentation by a code contributor to that framework or library, followed by a group effort fixing bugs while being assisted by experts. This way attendees learn a bit of the framework or library, the process of fixing bugs and the process of quality assurance (QA).

The first BugHuntDay is planned for November 8 in Roosendaal (the Netherlands) and that day will be devoted on fixing bugs on Zend Framework.

Check out BugHuntDay.org for more information or to RSVP for this event. Seats are limited, so first come-first served. Attending is FREE, so why not try it ?

Tuesday, October 14, 2008

Throwing and catching exceptions

I recently had to review a web application that was written in PHP and was using Zend Framework to facilitate in the site setup.

One thing that I noticed was that although the code was well written, implementing coding standards and best practices on many of the classes, I did notice a wrong usage of throwing exceptions (the try - catch statements). Most of the exceptions were captured in global class, but nothing was done with it. In this blog post I will try to explain the concepts of bubbling exceptions so you can manage it all in one place.

If you're new to the concept of throwing and catching exceptions, take a look at the description on php.net as it explains you clearly what it's all about.

In short, throwing and catching exceptions is the good way to make sure your application doesn't do stuff it isn't supposed to do and leaving your application broken.

Example: division by zero
function divide($value)
{
    return 1 / $value;
}


This example will return a fatal error when $value is 0 (zero).

A better way would be
function divide($value)
{
    if (!$value) {
        throw new Exception('Division by zero is not allowed');
    }
    return 1 / $value;
}


This will cause your application to be aware that an exception is thrown. Of course you need to handle this in a proper way, that's why you use a try - catch statement.

function mymath($value)
{
    $result = 0;
    try {
        $result = divide($value);
    } catch (Exception $e) {
        echo "An exception occured: " . $e->getMessage();
    }
    return $result;
}


But when you're working on a web application, this might not be your desired action because you don't want to let your visitor know your application is broken. Instead, you want to handle exceptions your own way, by bubbling exceptions upwards so you can manage exceptions in one place.

Bubbling exceptions means that you check in your code fragment for an exception (in your try statement) and throwing this upwards (in your catch statement).

function mymath($value)
{
    $result = 0;
    try {
        $result = divide($value);
    } catch (Exception $e) {
        throw new Exception($e->getMessage());
    }
    return $result;
}


When you create your own exceptions, you can perform better management on your exceptions and act differently upon them.
Creating your own exception is pretty simple, you only need to extend the Exception base class.

class DivideByZeroException extends Exception {}

That's all to have your own exception in place. Now you can bubble it upwards again.

function mymath($value)
{
    $result = 0;
    try {
        $result = divide($value);
    } catch (DivideByZeroException $e) {
        throw new DivideByZeroException($e->getMessage());
    }
    return $result;
}


Now you can create your exception handler method to handle each exception on it's own and perform a specific action. In this case you might want to log these exceptions, but when accessing databases, files or webservices you might want to send out an e-mail to the administrator or developers. I am sure you can think of useful ways to handle different kind of exceptions.

Another good read that's related to this topic is a recent blog post of Patrick Allaert titled Readable PHP code #1: Return ASAP.

Monday, October 06, 2008

Zend Framework Certified Engineer

Last Friday was a successful day for me as I passed the Zend Framework Certified Engineer (ZFCE) exam from Zend at a Pearson Vue testing center in Mechelen.

The exam covers 14 topics and launches 75 questions at you, mostly multiple choice but also a random free field for you to fill in.

Zend provides a Zend Framework Study Guide to prepare yourself for this exam, but as the exam covers many detailed components from the Zend Framework, the minimized version of this study guide only touches the most important components and common understandings.

The main categories are:
  • MVC
  • Forms
  • Databases
  • Infrastructure
  • Security
  • Coding Standards
  • Internationalization
  • Mail
  • Search
  • Authentication and Access control
  • Filtering and Validation
  • Web Services
  • Performance
  • Diagnosis and Maintainability
My advise for anyone taking this exam is to read the Zend Framework Study Guide and go through the Zend Framework Manual afterwards to study on the topics untouched or barely touched in this Zend Framework Study Guide.

If you doubting you're up for the challenge, PHP|Architect now offers training courses for this Zend Framework Certified Engineer exam. A Vulcan test will be shortly available.

Ibuildings, the leading PHP company in the Netherlands and UK provides also training courses for Zend Framework, but aren't targeted to the ZFCE exam (yet).
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License.