Pragmatism in the real world

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.

Global installation of PHP tools with Composer

The Composer package manager along with the Packagist repository site is quickly becoming the defacto PHP package management system. One feature I found out about recently is that you can install packages globally rather than locally into your project. I think that this is most useful for development tools, such as PHPUnit which are then available everywhere.

Using PHP's NumberFormatter to format currencies

I've been using number_format() for a very long time, but recently discovered that within the intl extension there's a NumberFormatter class available too. This is quite a clever class as it is Locale aware and handles formatting currency, including the correct symbol. You can check if you have the intl extension installed using php -m | grep intl and if you don't then you can install it with apt-get install php5-intl or yum install php-intl… continue reading.

Objects in the model layer: Part 2

I previously talked about the terms I use for objects in the model layer and now it's time to put some code on those bones. Note that,as always, all code here is example code and not production-ready. An entity My entities are plain old PHP objects: namespace Book; class Entity { protected $id; protected $author; protected $title; protected $isbn; public function __construct($data = array()) { $this->populate($data); } // Data transfer methods public function populate($data) {… continue reading.

Missing fields in $_POST

I recently updated to OS X 10.8 (Mountain Lion) which has PHP 5.3.13 installed by default. When testing something today, I discovered that a very very large form wasn't submitting all fields. It seemed that $_POST was being truncated. After a little bit of searching around I discovered the max_input_vars php.ini setting. This is new since PHP 5.3.9 and defaults to 1000. As OS X also comes with suhosin installed, if you want to increase… continue reading.