Skip to content

Date archive for: June 2015

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.