Pragmatism in the real world

Building and testing the upcoming PHP7

The GoPHP7-ext project aims to ensure that all the known PHP extensions out there work with the upcoming PHP 7. This is non-trivial as some significant changes have occurred in the core PHP engine (related to performance) that mean that extensions need to be updated. In order to help out (and prepare my own PHP code for PHP 7!), I needed the latest version of PHP7 working in a vagrant VM. Fortunately Rasmus has created… continue reading.

Convert PHP Warnings and notices into fatal errors

Xdebug version 2.3 was released last week and includes a feature improvement that I requested back in 2013! Issue 1004 asked for the ability to halt on warnings and notices and I'm delighted that Derick implemented the feature and that it's now in the general release version. It works really simply too. Turn on the feature by setting xdebug.halt_level either in your php.ini or via ini_set(): <?php ini_set('xdebug.halt_level', E_WARNING|E_NOTICE|E_USER_WARNING|E_USER_NOTICE); Now cause a warning: <?php echo… continue reading.

Sending test emails from PHP

I've written before about redirecting email while developing using a trap mail script on my development system. Other people also like MailCatcher. I've recently switched to using a simple PHP script that creates a elm file on disk that can be opened by Apple's Mail client. This solution is much faster as there is no SMTP processing involved. Adam Royle came up with this solution in his article Setup a testing mail server using PHP… continue reading.

Setup Doctrine Migrations to update MySQL timestamp on update

One project I'm working on uses MySQL exclusively and is also using Doctrine Migrations. I wanted to set up a column called updated that was automatically set to the timestamp of the last time the row was changed. This is done in SQL like this: CREATE TABLE foo ( id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(100) NOT NULL, updated timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY(id) ); It's not quite obvious how to do… continue reading.

Recursively deleting elements from an array

I had a need recently to delete items from a nested associative array and also any empty sub-arrays. My initial thought was to use array_walk_recursive, but this doesn't work as you can't unset nested elements and you only have access to the leaves. Clearly I needed a recursive function. I'm sure that this has been done many times before, but this is my solution: /** * Remove any elements where the callback returns true *… continue reading.

SSL certificate verification on PHP 5.6

I recently updated my local OS X Zend Server installation to PHP 5.6 and when I ran composer self-update, I got this error message: [Composer\Downloader\TransportException] The "https://getcomposer.org/version" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed Failed to enable crypto failed to open stream: operation failed Googling around, I finally worked out that there have been various SSL improvements in PHP 5.6 and that the problem… continue reading.

Overriding the built-in Twig date filter

In one project that I'm working on, I'm using Twig and needed to format a date received from an API. The date string received is of the style "YYYYMMDD", however date produced an unexpected output. Consider this: {{ "20141216"|date('jS F Y') }} creates the output: 20th May 1976 This surprised me. Then I thought about it some more and realised that the date filter is treating my date string as a unix timestamp. I investigated… continue reading.

Autocomplete Phing targets on the command line

Shortly after writing my last post, it crossed my mind that it would be nice if I could autocomplete the targets in my Phing build file on the command line. In order to do this, I read Buddy Lindsey's Quick and Dirty Write Your Own Bash Autocomplete and made it do what I needed! Start by creating a new bash completion file in the bash_completion.d directory. This file needs executable permission. This directory can usually… continue reading.

A few Phing tips

Following on from my last post, here's a few other Phing things that I've found helps me. Hiding targets from Phing -l I have a number of dependent targets in my build.xml that I don't want listed when I run phing -l. The easiest way to do this is to add the hidden="true" property to the targets I want to hide: <?xml version="1.0"?> <project name="buildfile" default="foo"> <target name="foo" depends="bar,baz" /> <target name="bar" hidden="true"> <!– tasks… continue reading.

Using Phing to SSH into a Vagrant box

Now that I've started using migrations, I've discovered a minor irritant. I run this project on a Vagrant VM and have discovered that I keep forgetting to ssh into the vagrant box before running the migrations script. The obvious solution is to automate this and I decided to use Phing to do so. Firstly, I needed to install the PHP ssh2 extension: $ brew install libssh2 $ sudo pecl install pecl.php.net/ssh2-beta I'm on OS X,… continue reading.