Archive for May, 2005

The Singleton Chapter

Posted 5/26/2005 By Jason

I was reviewing the editing of the Singleton chapter last night. It was kind of interesting to look it over again, as I originally wrote, and had the tech review performed, on this chapter back in December of 2004! Looking it over again I am pretty proud of the decisions I made in writing it. One important decision was to not make the Singleton pattern the first design pattern explained in the book. The actual sequence of chapters up to this point is:

  1. Preface
  2. Programming Practices
  3. The ValueObject Pattern
  4. The Factory Pattern
  5. The Singleton Pattern

So the Singleton pattern is the third pattern covered in the book. The Programming Practices chapter gave me a chance to introduce Unit Testing and Refactoring, which is very important considering the heavy use of SimpleTest throughout the rest of the book for unit tests of the code. As I mentioned in an earlier blog entry, the unit tests are not just included in the code download, but integrated directly into the prose and flow of each chapter. The ValueObject pattern discussion gave me a chance to review references in PHP4 and object handles in PHP5 in much greater detail, and doing FactoryMethod next lets me allude to the fact that the Singleton is essentially a specialized Factory which only returns a single instance of a class.

In the book I try to give equal coverage to PHP4 and PHP5, but this particular chapter I spend much more time with PHP4 examples, as having private constructors in PHP5 make implementing the pattern relatively easy. I cover several possible implementations, and highlight the fact that the Zend 1 engine does not store references in static variables (as noted in the manual).

I briefly cover the MonoState pattern here as well, since they end up exhibiting similar behaviors, and you can do some neat code with reference —non-object related references!—to implement the MonoState pattern.

Book Update

Posted 5/24/2005 By Jason

This post is just a quick update on the status of my book, PHP Architect’s Guide to PHP Design Patterns. Marco fired himself as editor…because he found a more suitable replacement. We are still kicking through the editing review and are about 20% of the way through the book, and are trying to process around one chapter a day. This is the final pass before layout.

As I understand it, the book will be available in PDF on the php|architect site very quickly after it has been through l lay out. The turnaround time for the print runs is minimal as well. To the best of my knowledge, we are still targeting June for having it available.

Marco has been hot on my tail with regards to some ideas to market the book. Keep tuned here for further information on this subject :)

Just the Facts Ma’m

Posted 5/18/2005 By Jason

What does a PHP geek do when confronted with the task of generating practice math problems for his eight year old daughter? Script it, of course. Madeline wanted to practice addition, so she gave me a crayon and told me to write down 100 math problems consisting of adding two single digit numbers.

A “better” solution lept to mind immediatly:

A class to store and render a “fact”:

<?php

class Fact {
    protected $top;
    protected $bottom;
    
    public function __construct($t, $b) {
        $this->top = $t;
        $this->bottom = $b;
    }
    
    public function render() {
        return '&nbsp;'.$this->top."\n<u>+".$this->bottom.'</u>&nbsp;';
    }
}

?>

Generate some facts and store them in an array:

<?php

$facts = array();

foreach(range(0,9) as $top) {
    foreach(range(0,9) as $bottom) {
        $facts[] = new Fact($top, $bottom);
    }
}

?>

Make sure they are in random order:

<?php

shuffle($facts);

?>

and render them into a table for easy layout (what can I say, it was a two minute hack):

<?php

echo '<table border="0" cellpadding="9">';

foreach(range(0,9) as $tens) {
    echo '<tr>';
        foreach(range(0,9) as $ones) {
            echo '<td>', $facts[$tens*10+$ones]->render(), '</td>';
        }
    echo '</tr>';
}
echo '</tr></table>';

?>

fresh, randomly ordered fact sheet ready for printing.

Here is the Test Driven Development in PHP presentation from php|tropics. You can download either the zip or tarball flavors. In each is the PowerPoint presentation and the code files with unit tests I used when writing the presentation.

This presentation also went well. I went out on a bit of a limb and did some TDD live for about 45 minutes near the end of the session. We all decided to work on a MySQL driven Guestbook. Since there were about 30 or so people who were willing to hang around for 20 extra minutes while we finished the example up, I think that is a pretty good testament to the presentation, particularly considering the beautiful Cancun sun which was minutes away from all the dedicated attendees.

Here is the Design Patterns in PHP5 presentation from php|tropics. You can download either the zip or tarball flavors. In each is the PowerPoint presentation and the code files with unit tests I used when writing the presentation.

I felt this talk went fairly well, particularly considering it was in the same time slot as part two fo Chris Shiflett’s Security presentation (which we all know how that one drew a crowd at php|works in Toronto last fall).

Breaking the Silence

Posted 5/2/2005 By Jason

Long time, no posts. I have been very busy writing my PHP Design Patterns book, and have left little time for (self|family|blogging|other activities).

First of all, every chapter of the book is done!! Every chapter had to be written, sent to tech review, edited, and then sent to the editors. The final chapter came back from tech review a week ago Sunday, and on Friday night I sent it off to the editors. As I understand the rest of the process, I have to do one more approval of the final layout, then after a short turn around, it goes up as a PDF on the php|architect website. Only a week or two after that, it will be available in hardcopy. Down to the wire, but I am hoping to see the dead tree edition by php|t.

I am really proud of the work which went into the book. The book is basically a pattern catalog. For each pattern, I tried to select an example which exemplifies the pattern in a web development context. Each example was then developed with complete unit tests in SimpleTest. After each example was working to my satisfaction, I would then decide how to show the code in the prose of the chapter. There were a couple of chapters which I basically rewrote from scratch after getting back comments from the tech review, and the results were much stronger chapters. I think the emphasis on testing; the use of both PHP4 and PHP5, and solid examples with UML customized to the examples and the patterns is going to position this book in a strong niche.

Speaking of php|t, all spare minutes are now devoted to polishing of my two presentations on “PHP5 Design Patterns” and “Test Driven Development in PHP”. If you are headed down to Cancun, look me up.

There was another request for code related to my previous book “PHP Graphics Handbook“. I have the code for the second half of the book in this zip file. As I mentioned in this previous post, if you still have access to the last code download, shoot me an email. If you know how to contact either of the other authors, I would be happy to know that as well.