PHP: The Good Parts
This blog post is inspired by Douglas Crockford's book JavaScript: The Good Parts.
All programming languages have warts. That is, there were certain decisions made about a language that are less than ideal. Some people are driven to remove these warts from the language in an attempt to make the language better. I think this is done with the best intentions, but can often have negative consequences. JavaScript is a good example of a language that has a lot of warts. Despite all these warts, JavaScript is a very useful language and has seen a huge rise in popularity. I feel PHP is the same way. It has bad parts, but there are so many good parts that we need to celebrate those good parts. Here are a few things off the top of my head:
Arrays
I think the array is the single most powerful and useful part of PHP. The PHP array is the Swiss Amry knife in my programming toolkit. I have written applications in a number of other software languages and I have yet to find anything else more useful. The best part about arrays is that they just work. I don't have to decide ahead of time between a list or a map. The PHP array is to data structures as NoSQL is to SQL. Better still is that PHP core uses them all over the place. Results from the database: arrays. Parsing a json POST from the client: arrays. They are ubiquitous in PHP in both core and userland. I cannot say enough good things about PHP arrays.
Web Ready
PHP is web ready. I do not mean that PHP is easy to integrate into a webserver. PHP is easy to integrate, but I think a lot of languages do a good job of integrating to webservers now. I mean PHP is built for the web. It is so easy to create an HTML template and pass the data to it. I think Mustache and Twig are great. That being said, I do not have to decide on a templating language in order to get up and running. Everyone understands HTML.
I do think this is feature is getting less important as the web develops. I write a lot of API's and send almost everything to the client via json. However, they are still tons of websites out there are that are not platforms and need to serve up HTML.
Streams
Streams are the best kept secret in PHP. Most people do not even realize they are using streams when they are interacting with file systems or networks. I wrote a plugin to push messages to the Phergie IRC bot in less than an hour using streams. They are a really powerful abstraction that is used all over PHP.
Type Juggling
For the most part, a web application is just a bunch of strings. HTTP is all strings, most database adapters return strings and all output is strings. PHP handles all of this and removes all kinds of boilerplate code from my applications. I think PHP has the most sensible implementation of juggling too. Yes, they are some problems with large integers that are represented as strings. It is by no means perfect. However, I think the PHP zval has saved me orders of magnitude more hours than pain.
For all the warts, there is plenty of beauty in PHP. I still enjoy writing web applications using PHP and focus my time using the parts of PHP that work really well.
Managing Gearman With Gearadmin Tool
The more jobs flowing through Gearman, the more likely something will happen. Queues can get backed up, workers can crash and performance can degrade. It is important to monitor the status of the Gearman ecosystem and be proactive about fixing problems. We can do this using the gearadmin tool.
Evaluate multidimensional arrays using vim xdebug
I love vim. I love XDebug. I need it to evaluate multidimensional (or nested) arrays though and it does not seem to do that. Chris Hartjes tweeted about his frustration with arrays too and I decided to fix the problem. Turns out there is nothing to fix.
Turns out we need to tweak a default configuration settings to get this all to work. Open up your .vimrc and add the following line:
let g:debuggerMaxDepth = 3
That depth means you will be able to view the contents of a triply-nested array. That seemed like a sensible default to me. Now you can evaluate or get the property of any variable like normal.
Once the depth is set there is no way to change it during a debug session. You have to close the existing XDebug session, update the value and start a new session. I plan on changing this in a future release of the plugin.
Using the Gearman Tool For Rapid Development of Clients and Workers
Gearman comes with a few tools that make development and testing easier. The gearman program creates boilerplate clients and workers. The gearman program comes default with the gearmand package. Do not confuse gearman with gearmand. The gearmand daemon is what manages the queue, clients and workers. The gearman program is a tool to quickly create simple clients and workers. The options for gearman can be slightly confusing, so I will go through a set of examples on how to use them.
Registering Functions With Gearman Workers
The Gearman examples on php.net are a great primer for groking how the Gearman client and worker interact with each other. One gripe I have is that the examples declare global functions for the worker to register. I feel this leads develpers down the wrong path. With PHP5.3, there is an easier solution though: anonymous functions.
The Painful Gearman Upgrade Path
The Gearman project has been slowly migrating from C to C++. This migration has gone under the radar due to the popularity of Cent OS 5 and given gearmand version of 0.14. This version of gearmand worked with any version of pecl/gearman and there was never any compelling reason to upgrade gearmand. That changed with the release of pecl/gearman 1.0
SPL FilterIterator in the real world
The Standard PHP Library (SPL) is a powerful set of tools that are often overlooked. It is very common to see an SPL talk at conferences, but those talks usually just introduce each SPL class to the audience without giving some real world examples. I am going to show you a real world example on how to use SPL FilterIterator in an ecommerce website.
PHP Gearman Bootstrap Script
Writing the scaffolding for gearman workers is a pretty trivial task using the pecl/gearman extension. Keeping that scaffolding consistent between all the gearman workers in your application can get tough. I created a script that will remove the boilerplate gearman code and allow gearman worker scrips to simply be function definitions.
PEAR channel created
I finally got around to creating my own PEAR channel. I am hosting it on github using Pirum to manage the channel. The channel is located at http://hradtke.github.com/pear/. So far I have a single package in then channel: gearboot. I will be writing more about gearboot shortly.





