Design Patterns in PHP

php design patterns and idioms

Following my petty wingeing about the design patterns in php on the web I have to bite the bullet and do something about it. So here it comes a collection of design pattern examples, some from the GOF book, some collected from around the net, some (maybe) I have discovered. I have to admit, I had an eye opener, when I was rereading Peter Norvig's presentation on Design Patterns in Dynamic Laguages. I shamelessly lift a lot from there, should I quote everything?

The intent of these pages is to show the shapes of the patterns not a fully fledged implementation. In real life probably a combination of patterns will be employed. Everyone has their own favourites following their political, religious and style differences.

It is a work/study in progress, so expect changes and additions. Some of the patterns are transparent in php, but are given for comparison with other implementations.

This is not a pattern fundamentalist site, so no single interpretation is considered the one and only truth

update I've updated the list with a number of other design patterns. To the trolls, sorry purists, - yes, I'm aware that I smudge the boundaries between design patterns, idioms, techniques, ... That is intentional. The world is not black and white, not even grayscale.

another update I moved all of the pattern catalogue into the shorts section. I plan to add longs, I should imagine.

why?

Hi,

Yes there is a slew of php pattern libraries emerging.

I like your attitude to smudging the edges of patterns, idioms, though I am not experienced enough to understand it.

"Showing the shape of patterns" I like that (actually I need it).

If I am researching how people use a particular pattern, then I will stop by your site.

It sounds quite irksome, but personally I cannot begin to really grok a pattern until I have used it (or abused it, more to the point).

But can I ask:
Why are you maintaining this list, do you have a particular audience in mind? if so can you describe them?

short - why not

Long, things get more complicated. If you browse around the site - you'll see that I've been logging some of the tests/experiments I've been doing. Then I stumbled upon a bunch of example pattern code which is not wrong, but obviously too verbose. The reason, I suppose is that people try to understand, me including, what that pattern means, how to code it, look at examples, then do it. More often than not, they translate form Java/C++/C# - which whoul be fine, in the end it works for those languages, but ,and this is the big point for me, php has features not present in those languages.

It is a dynamically typed language, with a couple of unique features - like the ability to freely convert between strings and symbols (variable and function names). It would be foolish not to use those. In my opinion, we should code the pattern in the language, and more importantly, this helps the understanding of the language. Speaking it better so to say.

Patterns are not self-contained standalone entities. To be honest, I wrote and uploaded this collection, simply because I got pissed off. Then I said to myself, yeah, you can throw tantrums, but can you do better? What is better? How do you see these things implemented? What do you use for your own code? Then a few of the "standard" ones, and a couple of the items on the list - for completeness - they are too short or copies of other patterns to be useful, but I wanted to show that similarity.

Audience? Well, myself and my vanity for starts, a few of my friends with whom we exchange ideas, people like you. I simply don't believe that I should keep knowledge in a cupboard. Even if this helps a little, it's worth it, because it wil bounce back to me.

your wise words

I'm sorry I havent browsed around the site enough, will make a point of doing so (I see you're a fan of jonnay/bunny too).

I tell you why I ask the question about the audience. If I was making a patterns website for me (at my stage of learning, ie not scholared, a JS/html monkey turned php hacker, reading tooooo many threoretical pattern books) Then your thoughts hit the spot:

"....php has features not present in those languages.

It is a dynamically typed language, with a couple of unique features - like the ability to freely convert between strings and symbols (variable and function names). It would be foolish not to use those. In my opinion, we should code the pattern in the language, and more importantly, this helps the understanding of the language."

Its not only that though, PHP is borne of, and operates in, this stateless, hostile environment that is the internet.

Its refreshing to see someone saying 'Lets see how PHP can "play to its strengths" rather than keep knocking how its not as good a Java/etc'.

Like I said previously, theres lots of real PHP pattern sites springing up, extolling reams of code, and that is fine.

But back to my point again...

I would really like to also see discussions/lists containing examples of when a particular pattern was used.
E.g Observer:
this is its shape, here it is as functions only...
Used it in a Transaction Script for logging... etc

Especially if you can elicit this kind of feedback from your visitors: "Even if this helps a little, it's worth it, because it wil bounce back to me."

I hope so.

observer pattern

Well, not exactly the observer pattern, but a close relative. One of the core concepts of the Drupal apis is something called hooks. To cut the long story short - it defines a function naming convention, i.e $module_name . '_' . $hook and hook invoking functions. The idea is that whenever you invoke a hook, the system will automatically discover and call all currently available ones. Actually that is the reason I define the Hook pattern and name it accordingly. You can find closer relative in my earlier aspects and honey post, where the code works/is closer to working :) (you can never be really sure).

I agree that there should be websites showing proper usage of a particular pattern. The problem is that these patterns are a bit artificial. Very rarely you will meet them on their own. Why do you think I was able to generate these pages so quickly? (It took me around 3-4h to write the examples from scratch and I'm not a quick typist). The main reason is that I've spent some time in the past to use some of these patterns and idioms and I've decided to approch this catalogue as quick sketching of solutions to previously defined problems. That's why the format. Then other things started coming to mind.

And thanks for the wise

Powered by Drupal, an open source content management system