Skip to content

PHP 4 Constructors and PHP5 Namespaces Don’t Mix

Using a class defined in a namespace will disable a legacy Constructor.
All classes in namespaces *must use* __construct.

You might run into this problem trying to convert an old library to be PSR-2 (autoloader) compliant. If you want to use a legacy library in Laravel or Symfony, you’ll have to watch out for this.

Batching Inserts by Combining Queries

Obligatory warning: Always escape your inputs! This assumes you are migrating known safe data!

I see lots of legacy code (especially bulk data migrations) that perform database operations inside a loop. Like this:

This results in a series of queries:

but we can do better. SQL inserts can be stacked into one query:

This still inserts all 3 records, in just one transaction. And it’s easy to retrofit our existing code:

A little more code of course, but this will speed up your inserts dramatically.

Tired of dealing with Leap Years? Use DateTime::diff()

Doing this the old-fashioned way was pretty complex. It was bad enough when a normal February would mess up your math, you also had to take leap years into account. Using the DateTime functions in php >= 5.3 lets you think about your business case, not calendars.

Don’t take my word for it! **Test it**!

For more info see the php docs:

Fake Multiple Inheritance

Leveraging code reuse in a world without mixins

First, we need our interface. All the other objects conform to this interface, so they are all polymorphic.

The DiscountCart is the most primitive of the Discount types. It gives the customer a 15% discount on their entire cart. Inheritance.

The VolumeDiscount inherits from the DiscountCart, and overrides with its own discount based on the volume of each product in the cart. Inheritance with a method override.

There is another discount which adds free shipping. Inheritance with a property override.

Now, there is a very special customer type: the Frequent Customer. We give them a Volume discount AND they get free shipping all the time.

If we had mixins, we could just “mix in” the free shipping, but since this is php (or C# for that matter) I do it this way:

This lets the FreeShippingDiscountCart be compatible with any IDiscountCart polymorphism and leverage inherited traits from its private objects. That way, if Free Shipping gets complicated (ex: drop-shipped or sale items are not eligible) I don’t have to re-implement the exceptions.

Add page slug to class list using wp_list_pages()

Add this code to your functions.php file.

Ordinal Suffixes in PHP


Counting Characters: PHP vs Javascript

On a recent project, an issue cropped up where we were exporting xml
reports to a third party, who had rules about line lengths in
Description fields.

So, I wrote up a quick-and-dirty character counter for the description
field so the client can keep their descriptions under the 1500-word

Here’s how it was done:

Now the html and javascript:

All was working well, except I underestimated the descriptions. The next
morning, the thing was broken. The php side was kicking validation
errors (“over 1500 characters”), while the javascript was counting a
mere 1480. Why?

Turns out the data was full of bullets and other multibyte characters.
To make the counts match, I needed multibyte-safe php:

Hooray! Now the counts match!

Use Firebug to Update a Huge Form

Had a problem recently where I had to update hundreds of input fields within a shopping cart, setting prices of product variants from $200 to $49.

They were named in sequence:

So I busted out the command line in Firebug.

This saved literally hours of


Thank you Firebug!