Pragmatism in the real world

Registering Doctrine Type Mappings for standalone migrations

Shortly after starting to use Doctrine Migrations as a standalone tool in my project, I came across this error message: Unknown database type bit requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it. This means that I have a column in my database of type bit which is used for booleans in SQL Server, but confuses the MySQL platform as it's not a default mapping. To support this, you need to modify the database connection's Platform object to… continue reading.

Using Doctrine Migrations as a standalone tool

My current project has reached the point where a good migrations system is required. As I'm targeting two different database engines (MySQL and MS SQL Server) and we're already using DBAL, it made sense to use Migrations from the Doctrine project. To do this, I updated my composer.json with the following require statements: "doctrine/migrations": "1.0.*@dev", "symfony/console": "~2.5" in order to install Migrations and also Symfony console component so that I can run it from the… continue reading.

substr_in_array

No matter what I want to do with an array, PHP usually has a first class method that does it. I was therefore surprised that in_array() didn't handle substring matches. (I was sure there was a flag, but apparently not!) No doubt everyone has their own version of this, but here's mine so that I don't have to recreate it next time: /** * A version of in_array() that does a sub string match on… continue reading.

Setting up PHP & MySQL on OS X Yosemite

It's that time again; Apple has shipped a new version of OS X, 10.10 Yosemite. Apple ships PHP 5.5.14 with Yosemite and this is how to set it up from a clean install. However, if you don't want to use the built-in PHP or want to use version 5.6, then these are some alternatives: PHP 5.3/5.4/5.5 for OS X 10.6/10.7/10.8/10.9/10.10 as binary package by Liip Homebrew has PHP. Zend Server 7.x (Paid for) Let's get… continue reading.

Alias for the PHP built-in server

I keep forgetting the correct command line syntax for the PHP build-in server, so I've now made an alias for it in my .profile: alias phps='php -S 0.0.0.0:8888' Now I can simply type: phps public/index.php to start the built-in web server.

Throw an exception when simplexml_load_string fails

I keep having to look up how to stop the warning that are emitted when simplexml_load_string & simplexml_load_file fail, so this time I've written the world's simplest little class to take care of it for me from now on: <?php namespace Rka; use UnexpectedValueException; class Xml { /** * Load an XML String and convert any warnings to an exception * * @param string $string * @param string $class_name * @param int $options * @param… continue reading.

Speaking at DPC & OSCON

Two conferences are coming up that I'm speaking at: The Dutch PHP Conference takes place in Amsterdam and this year is between June 26th and 28. I'm giving a tutorial with Matthew Weier O'Phinney on Apigility which will provide you with a full day's teaching on creating web APIs with Apigility. I'm also giving a talk called Creating Models discussing the options available when creating the model layer of a typical MVC PHP application. DPC… continue reading.

Immutable entities

I've been thinking recently about using immutable objects for some of my model-layer entities. That is, an object that cannot be changed once it is created. Immutable objects have a number of benefits including the fact that they are much easier to understand, simple to use and very easy to test. The main motivation for me is that an immutable object is very predictable as it's state doesn't change. This means that any calculated properties… continue reading.

Use statements

I was having a discussion on IRC about use statements and whether they improved code readability or not. The choices Consider this hypothetical code: $cache = new \User\Service\Cache(); $mapper = new \User\Mapper\User($cache) $form = new \User\Form\Registration($mapper); $form->process($request->getPost()); vs use User\Service\Cache; use User\Mapper\User; use User\Form\Registration; // other code $cache = new Cache(); $db = new User($cache) $form = new Registration($mapper); $form->process($request->getPost()); The first snippet is completely unambiguous at the expense of verbosity. Those longer class names… continue reading.

Creating a zip file with PHP's ZipArchive

I recently had a requirement to create a zip file from a number of files created within my application. As it has been years since I last had this problem, I had a look around and discovered that PHP 5.2 has the ZipArchive class that makes this easy.