Book Review: PHP Hacks

Book Review: PHP Hacks by Jack D. Herrington, published by O'Reilly

I had borrowed a Perl Hacks book from and friend and really liked it, it was great! It had a lot of practical things as well as some fun things. I expected the same from PHP Hacks and I was not disappointed!

Here’s the table of contents:

O’Reilly also has some sample hacks:

Here's some that I found interesting:

The Practical Stuff

Not familiar with the term? Check it out. I think this hack may get you started on a breadcrumb function/method. In the end they suggest a xml file to show which page urls were parents of which. The way I've done this before was I had a class for each major section, and sub section, and had a method ->addCrumb($label, $url) which I had in the constructor…and the subsections of course would call the parent contructor and it kept the breadcrumb hierarchy intact. But hey, that’s the fun of programming - different ways to do the same thing to meet different needs! Definitely a good hack to get the juices flowing!

Building Lightweight HTML Graphs
Don't want to use flash to display a graph? use PHP to figure out the ratios and give you a width and use a table. I've done something similar by figuring out the width of a div, making the background a color… and I have used 1x1 pixel images that have been stretched to a certain width and height. It’s a very lightweight download for your user, that’s for sure! Later in the book, there's a hack for creating a Dynamic HTML Graph that will change without reloading the page.

Put an Interactive Spreadsheet on your Page
This one is so cool -- you need to read about it in the book yourself! Lets says, move over Google Spreadsheets! We can do it too!

Create Link Graphs
I call these Tag Clouds, not sure why they call them Link Graphs here in the book -- probably, tag clouds has been copy written by some Web 2.0 smartass. Here I am, sue me! This is a unique and visual way to show the popularity of certain words in a group. Rather than a numbered list, this is visual. I had this discussion not too long ago with a group and sadly, most of them didn't get it. I think if I actually used this sort of technique on a page, I'd include a "What's this?" link or an alternative view.

Create Dynamic Database Objects
This was very interesting to me because I love Active Record in Rails. This relies on some of the magic of PHP5 to work, probably this is not going to be the best performance code but really -- is anything easy the fastest?

Generating CRUD Database Code
Similar to previous, but a create-once and go method, this hack will read from a xml file and create CRUD objects for maintaining your database. These will probably be faster then the previous one -but you'd have to run this script or update manually when your schema updates. Some people hate code generation - some don't. Pear's DB_DataObject is a similar concept.

There are a few other nifty database hacks making this my favorite section of the book!

Turn any Object into an Array
Using foreach is my default iteration function and using the PHP 5 iteration interface on any object to give it that functionality is awesome. This is one of the most practical design patterns (other than the other favorite: singleton) that I talk about to people who ask me - what are design patterns and why should I care? Speaking of design patterns - tired of reading a design pattern book and trying to figure out the smalltalk or java code? the design pattern section of the book has diagrams and sample code that you can understand.

Fun Stuff

Build a DHTML Binary Clock
What is that you say? Take a look at ThinkGeek and learn how to make something similar in DHTML. Yes, this is not practical but its fun. Something fun is always a great way to get excited about programming and enjoy yourself.

Generate Your Unit Tests
I put this in the fun section because testing IS fun - I love it. One of the things people (normal people, not wacky test freaks like me) complain about testing is - no time! Here's a hack that will let you put your test in a comment and running this script on it will pull out those comments and write your test for you. Nifty!

Build GUI Interfaces with GTK
I've always wanted to try something with GTK, but never have. This hack shows you how to build a regex expression tool to test and play around with regex. I've had programs like this and they are darn handy when you want to do a quick check. So, after you've had your fun building this -- its practical as well.

Send RSS feeds to your IM Application using Jabber
Depending on your use, this may in fact be practical but I think its rather fun. I have not figured out a use for this myself but the book uses it to send weather forecasts to your IM client. Neat-o

What I didn't like about this book
not too much in the way of checking input variables. I know, probably they "leave that as exercise to the reader" but noobs and experts alike need to get this ingrained into the head by default. So I suggest also picking up Essential PHP Security

What I like
ER diagrams and control flow - Nice!
Handy size

I've only touched on a few of the great hacks and there are many more I also like, but didn't really want to give away the entire book! I suggest you pick it up and keep it handy. Next time you are bored -- flip though and find something interesting!