Category: PHP

How I won the book “You want to do what with PHP”

Yesterday I received the news I was the lucky winner of the book “You want to do what with PHP” by Kevin Schroeder.
Kevin works for Zend Technologies as a Technology Evangelist and
is also the co-author of The IBM i Programmer’s Guide to PHP.

The book “You want to do what with PHP” has what I would call one hell of a cool cover. I do not know what it is with technical books, but the tendency
is to put very old drawings of woman, animals, artifacts, constructions (mostly bridges) and so on on the cover. I do not know why they do that. Maybe it are metaphors, if so I only understand the construction one. An antique sketch of a woman is less clear, maybe it says beware the programming language you will read about is soooo static and old it will crumble your mind. HAH, as if! PHP is dynamic and packed with fun cool things, so why not make the cover dynamic as well? Well this book cover portraits just that. Judge for yourself.
You want to do what with PHP

If you want to know what this mind stretching book is about check Kevin’s blog:

The book was on my Amazon’s wish list when I saw a tweet come by…

People who know me, know I am active in the PHP Community and that means I was able to see a tweet come by from kpschrade aka Kevin telling everybody who follows him or #php to visit a blog post he wrote: the big give-away. He announced he was going to give 9 free copies to the community. Three for people who are fan of his book on facebook, 3 for people who would retweet the blog post and 1 copy to each of the 3 lucky PHP user groups world wide. That made me have 3 possible venues where I could win a copy. Potentially through my local user group PHPBenelux (if they would win), facebook and twitter.
It was Facebook that did the trick. Now I am looking forward to receive it after which I will write a full review on this blog.

You can also win a copy as Kevin only recently started the big giveaway so hurry up:

  1. tweet the blogpost
  2. become a fan on facebook
  3. if you are not yet a member of a user group shame on you, wash the shame away and go join one (! The reasons why are neatly summed in
    the blog post by Anna

Thanks Kevin for the book,
Warm winter greetings from Belgium,
Nick Belhomme

PHPUnit Training Course for free

Today is an awesome day my dear fellow programmers.

Today is the day I decide to freely distribute the first couple of pages of the PHPUnit workshop I have been giving for some time now. It is a workshop that builds a small demo application the Test Driven Development way. The reason I open this workshop course is because I strongly believe everybody should understand and use PHPUnit in their projects. Another reason is marketing. As a Zend Framework Certified Engineer I am writing a wonderful book. Part of that book will have similar contents as the complete course, which today I decided to freely distribute. Yet those contents are only available when you attend the workshop (in which case you get a free copy of the entire PHPUnit e-book) or when you purchase my upcoming book, which will be available in pdf and paper format (in a bookstore near you).
Shamelessly I hope this free sample will peak your interest in either one of those.

But the bottom line: you get a nice introduction to PHPUnit for free. A course which has cost me many hours over several days to write. Hard work done with love, and with that love I share with you:

Warm winter greetings from Belgium

PHP 5.3.3 Namespaces

Purpose of the entry

With PHP5.3.3 recently released I really feel it is time that php developers are taking namespaces seriously. If you don’t I guarantee you will be out of a job within five years. Namespaces are a fundamental part of the future of PHP.
The big frameworks like Symfony and Zend Framework have understood this and build their latest versions (2.0) with namespace support.
You all have namespaced before using the filesystem and long class names. This is still a valid way to go, but as time evolves you will see this less and less. Probably the filesystem structure will remain but not the long tedious names. We will start to use aliasing or namespace scopes for that.


  • Getting started with namespaces
  • resources

Getting started with namespaces

So how does one begin with namespaces? Well simply by doing it. And what better way do to it than by example.
In this blog post I will explain how to use namespaces in a project and how to import two external libraries into it.
One your own and the other a real third party.

It is a small project and solely for demonstration purposes, kept intentionally very small but explanatory by nature.
So do not heckle me for design decisions and such, those would only cloud the goal of this small presentation.

You can download a working copy of the project so you can play with it and experience namespaces first hand.

I will wait a minute or so.

Done and loaded in the browser? Great, let’s get on with it.

As you see I first put all code outside of my public folder (httpdocs). I have put everything regarding setting include_paths
and constants in Application.php.

// Application.php
namespace {
    ini_set('display_errors', true);

    function __autoload($class) {
        $class = str_replace('\\', '/', $class);
        $class = preg_replace('/^Application/', 'application', $class);
        require_once $class.'.php';


namespace Application {
    const PROJECT_PATH = '../';

    $bootstrap = new Bootstrap(new \Nick\PlayBowl());

The file itself contains procedural code with two namespaces. You can attach as many namespaces as you want inside a file by encapsulating them between brackets. The namespace operator must be the first thing defined in a file.
In Application.php we have a global namespace in which we will register the autoload and setup the include_path. Autoloading namespaces
has to be done in the global scope. It also makes sense to set the include path in the global scope. Everything related to our application
itself goes into the Application namespace.

We will load the Bootstrap class from the namespace Application. Class names that do not contain a backslash like Bootstrap can be resolved in 2 different ways. If there is an import statement that aliases another Bootstrap to Bootsrap, then the import alias is applied.
Otherwise, the current namespace name is prepended to Bootsrap. Because we did not use the use operator no aliasing or importing is done, and Bootstrap will resolve to Application\Bootstrap().
Because no leading backslash is used when we defined the “namespace Application” we have specified an unqualified name. PHP will resolve this by adding the namespace to the current used namespace. In this case it is the \ (global) namespace.

// Bootstrap.php
namespace Application;
class Bootstrap
    protected $_game;

    public function __construct(\Nick\PlayBowl $game)
        $this->_game = $game;

    public function _setupSomeUglyNamedNameSpace()

    public function run()

Inside the Bootstrap.php file we have used the namespace operator to register the class Bootstrap under the Application namespace. We
did this by opening Application as the current namespace and then adding constants, functions and or classes to it. In our example the class Bootstrap.
Making it available by calling it from outside the current namespace using \Application\Bootstrap(); Just as we did with \Nick\PlayBowl in Application.php. As you can see we can also force the type in the constructor using namespaces just as we did with long class names.

In \Application\Bootstrap we use already 3 namespaces: Application, Nick and SomeUglyNamedNameSpace to target specific functions, constants and classes.

we have two different use cases for the namespace operator. Outside a namespace we switch the scope to a new namespace using namespace nameOfScope and inside a namespaced scope we can use the operator to target the current scope. This making the namespace operator the equivalent of the class self:: operator.

We have registered an instance of \Nick\Playbowl to $this->_game and we call its methods in the run() method. Let us take a look at that.
Because here you will see aliasing.

namespace Nick;
use \SomeUglyNamedNameSpace as Lib;
class PlayBowl
    public function aim()
        echo 'you are aiming at the pins<br />';

    public function roll()
        echo 'you release the ball and wait in anticipation<br />';

    public function getHits()


    protected function _hit()
        $calculator = new Calculator();
        if ($calculator->isGood()) {
            echo 'you hit all pins<br />';

    protected function _hit2()
        $calculator = new namespace\Calculator();
        if ($calculator->isGood()) {
            echo 'you hit all pins<br />';

    protected function _getPinsHit()
        $someCalculator = \SomeUglyNamedNameSpace\Calculator::factory(\SomeUglyNamedNameSpace\Calculator::SCIENTIFIC);
        $num = $someCalculator->add(2, rand(2,4));
        echo sprintf('%d pins hit', $num),'<br />';

    protected function _getPinsHit2()
        $someCalculator = Lib\Calculator::factory(Lib\Calculator::SCIENTIFIC);
        $num = $someCalculator->add(2, rand(2,4));
        echo sprintf('%d pins hit', $num),'<br />';

    protected function _getPinsHit3()
        $someCalculator = new Lib\Math\Addition\Scientific();
        $num = $someCalculator->add(2, rand(2,4));
        echo sprintf('%d pins hit', $num), '<br />';

    protected function _getPinsUnexisting()
        try {
            $someCalculator = Lib\Calculator::factory('science');
        } catch (Lib\Exception $e) {


Ok a lot is going on in here. Or so it seems… What I actually have done is included a lot of duplicate code for your pleasure.
let us first take a look at hit() and hit2(). Those two are identical. My preference is the hit() method. I will explain.
This method is identical to hit2, but this function doesn’t use the namespace operator, because by default PHP will prepend it with the current namespace. In hit two we explicitely use the namespace operator and so we manually prepend the current namespace to the classname. In hit2 the namespace operator is the namespace equivalent of the self operator for classes.

Time to take a look at the _getPinsHit methods. Again these are example methods. Were in _getPinsHit we use the third party library SomeUglyNamedNameSpace and use the fully qualified namespace (leading backslash in the namespace).

_getPinsHit2() does exactly the same thing but here we use the namespace aliasing functionality. We have at the top of the file defined the
namespace alias with the use of the use operator. use \SomeUglyNamedNameSpace as Lib; Thereby effectively shortening that uglynamespace or preventing namespace clashes.

_getPinsHit3() goes further into that example by showing that you can easily use that aliased name as the base for nested namespaces.
Note that you can not have real nested namespaces like you can have nested ifs. You can only have nested namespaces like you can have nested classnames. Thus by appending the namespace names, extending the scope. Like in this example. The class Scientific is a part of the namespace Lib\Math\Addition\ which in his turn is a part of \Lib\Math\ and so on…

_getPinsUnexisting Doesn’t really demonstrate something in this class but I needed to show you the difference between global namespace functions and other namespaced functions.

But first let us take a look at the other usage of the use operator. The import usage. This will be used a lot to shorten names.

namespace SomeUglyNamedNameSpace;
use SomeUglyNamedNameSpace\Math\Addition;
class Calculator
    const SCIENTIFIC = 'scientific';

    static public function factory($type = 'scientific')
        if (self::SCIENTIFIC === $type) {
           return new Addition\Scientific();
        throw new Exception('you must specify a valid Calculator type');

We used to do “return new SomeUglyNamedNameSpace_Math_Addition_Scientific();” , now with namespaces we can finally shorten it with:
use SomeUglyNamedNameSpace\Math\Addition; and from then of on you can call in the current scope simply “return new Addition\Scientific();” Which makes your code much easier to read.

Ok of to our final piece of example code:

namespace SomeUglyNamedNameSpace;
class Exception extends \Exception
    static protected $_logFile;

    public function log()
       file_put_contents(self::$_logFile, 'this will call the namespaced function');
       \file_put_contents(self::$_logFile, date('H:i:s')."\t".'this will call the global function, which we all know');

    static public function setLogFile($path)
        self::$_logFile = $path;

function file_put_contents($filename, $string) {
    echo 'this is '.__NAMESPACE__.' function';

I know in good design the last function should not have been in that same file, but this example if for the understanding of namespaces and nothing else. It isn’t a real Bowling game either 😉

Ok what is going on?

We first Extend the global Exception class which we all use everyday and implemented a log() method.
This log method does two things. It first calls the file_put_contents as an unqualified namespace (no backslash operator in the name found).
As before this will have the effect that it will first try to prepend the current namespace to the function and thus will find the function at the bottom of the file. And secondly we call explicitily the good file_put_contents, the global scope version! Which will nicely log the error message to the log file setted in our Bootstrap.php

I hope this small presentation on namespaces made a lot of things clear.


See you soon,

Nick Belhomme

Dutch PHP Conference 2010 (#dpc10)

More Photographs / enlarge

Purpose of the entry

For those who don’t know me, I always do a small write-up on each conference I attend (it is to persuade all you non-attendees to be there next year and maybe say hello).
This write-up will be on an incredible event packed with lots of interesting topics, relaxing atmosphere, interesting people to meet AND goodies to win. In short the Dutch PHP Conference. And this year it got even better.


  • What is #dpc10?
  • Yes! I spoke at the Dutch PHP Conference
  • Who was attending?
  • Pros and Cons of this year
  • Conclusion

What is #dpc10?

The Dutch PHP Conference is an event organized by ibuildings and spans 3 full days.
One tutorial day and 2 conference days. Generally the talks cover the more advanced PHP related topics. This year was the fourth installment of many more to come and was hosted by Lorna Jane Mitchell.
Compared to the previous years there was a new feature: the Uncon organized by PHPBenelux. The uncon is a less formal main stage at the conference where beginning and experienced speakers can bring their topics under public attention. You get requested the same day to speak in a certain time slot or you can volunteer to talk. If accepted your name will be listed next to the main speakers topics.

Yes! I spoke at the Dutch PHP Conference

I spoke about the good practice on separating different responsibilities of your application. It focused on

  • a model is NOT your database
  • how models retrieve results in the form of collections and entities from your Data Access Object (Dao, this might be
    the database logic or a web service).
  • Why it is a good idea to implement using a factory (or even dependency injection) and not hardcode your dao inside each model.
  • Creating a dao interface (contract based design) so that when you want to switch to another implementation, the model requires no change.
  • that with this system (what OOP is all about) you create clear separation and unit testing is made very easy.
  • you promote loose coupling
  • Easy creation of extra layers by the use of decorators

It was a completely unprepared lecture, because it was a “Hi Nick fancy to speak” kind-of-a-thingy. I am glad I accepted because I received very positive feedback and generally people wanted more. You can see the comments on my talk on

Who was attending?

Approximately 400 attendees with the “open source state of mind” at heart joined the conference. People who love to learn or lecture
on everything programming related. It is about so much more than PHP. I urge you to join even if you are programming into a different language like .net or java. There is a wealth on information at this event and you feel the positive vibe of each and every attendee.
The sponsors gave away some cool gifts too, with Microsoft taking the lead. The raffle included a full week to Las Vegas, an XBox360, programs and much more. Microsoft is really starting to embrace the PHP Community by sponsoring almost each PHP Conference, for which the community is very grateful.

Pros and cons this year


  • better stage venue
  • lots to learn at great lectures
  • lots of awesome people who are happy to meet you
  • an awesome feature called uncon sessions which is already fully adopted at United State conferences
  • conference social drink at a local pub sponsored by GitHub => Free drinks for everyone


  • like every year, the food is a bit disappointing. Prepare yourself to eat very basic sandwiches for 3 days. (The organizers could learn something from PHPUK, they serve awesome food, see my previous post)

Closing notes

Great value for money and in general a conference you should attend, no excuses.
See you next year at The Dutch PHP Conference 2011 (9-11 June 2011).

Thx for reading and feel free to comment,
Nick Belhomme

Dutch Audience