Quick strace command that I use all the time to see what files a process is opening:

strace -f <command> 2>&1 | grep ^open

Really useful to see what config files something is reading (and the order) or to see what PHP (or similar) files are being included.

There’s normally other ways to do this (eg using a debugger) but sending strace’s stderr to stdout and piping through grep is useful in so many cases it’s become a command I use every day or 2.

Better Dependency Injection in Laravel

We all love using Dependency Injection but often Laravel’s facades and IoC container are used more like a static service locator, which (despite being testable) can easily lead to violations of the Dependency Inversion Principle (the D in SOLID).

There’s been a lot of discussion around this in the PHP community recently, with calls to stop using facades as well as how service locators can be used safely (the comments here are good too).

The great news is Taylor, the main author of Laravel, has responded to this and made a change to Laravel in v4.1.15 that makes it much easier to inject dependencies directly into controllers.

I’m happy to see constructive criticism actually influencing and benefiting a framework so positively. Hopefully we’ll see more if this in the PHP community!

Shared mobile network tethering

Right now there are millions of people with mobile data plans that allow tethering and are being unused. There are also many people in need of an Internet connection.

Fon” sllows people to share their home / business WiFi connection publicly and monetize it. What if Fon (or a similar service) allowed you to share your mobile data (3G or 4G) publicly and securely? This could lead to WiFi access being available wherever there was mobile data access.

A few problems:

  • needs a critical mass (Fon already has this)
  • needs to work on smartphones (it would work on rooted Android devices)
  • needs devices with good batteries
  • needs data plans that allow tethering
  • needs to have really good pass-off between hotspots (there’s lots of interesting research happening in this area)
  • it isn’t long term (eventually everywhere will have WiFi / WiMax)

Unlimited passion

In this digital society we are so fortunate. We have tools and services available to us that allow us to build almost anything our imagination can think of. Not only that but we can share these creations with thousands of like-minded people in seconds.

The proliferation of low cost computing devices and ubiquitous Internet connections over the last couple of decades means that billions of people in the world now have access to a device they can use to create “stuff”. The increase in breadth and quality of Open Source software has also played a large part in this.

As developers we have all the tools available to us to write code and put it on the web. We’re limited primarily by our own time and our passion to learn. Not only does the web have all the educational resources required for us to program available for free but services like Github allow us to show others the things we create and foster collaboration and community.

In some ways technology and the web is creating a level playing field for people who build things. The passion developers have for their craft is what shines through.

Conway’s Game of Life in Canvas

I haven’t done much with Canvas, so I thought I’d experiment by making Conway’s Game of Life with a little JavaScript. Conway’s Game of Life is based on a grid of cells, which are either alive or dead. It has 4 simple rules rules:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction
These simple rules allow anything to be created. For example:

Gliders in Conway’s Game of Life (from Wikipedia)

You can see the code on GitHub. It’s very basic but works OK and isn’t too bad for a couple of hours whilst watching TV (it may eat up your CPU a little, sorry). Pull requests, ideas and suggestions welcome.

You can see it in action in the header of this blog. If you move your mouse over it then it will leave a trail of blocks. Clicking on the canvas will add lots of random cells.

Bitcoin: the cryptographic currency

Bitcoin is a digital currency without any central control. It was introduced 4 years ago today (3rd January 2009). 1 Bitcoin (BTC) is currently worth around $13. Last year Bitcoin grew considerably in market cap (now about $150 million) and popularity, with many organisations accepting Bitcoin as a form of payment.

As a software developer interested in security, the idea of a distributed digital currency with foundations in cryptography is fascinating. Read on if you want to find out more about Bitcoin or just have 5 minutes to spare.

Continue reading

Generators in PHP 5.5 (with PDO)

As the first alpha of PHP 5.5 has been released, now is a great time to play with its new features. The first new feature is generators. Generators are like very simple iterators.

PDO’s PDOStatement class implements Traversable, which means you can iterate over it with foreach:

$sth = $dbh->prepare("SELECT * FROM users");

foreach ($sth as $result) {

This is great as it abstracts away much of retrieving result sets from a database and gets you straight to an array-like structure. This iterator is great as PDO only retrieves the results as they’re requested, reducing memory usage.

You can recreate this by making a class that implements Iterator and its abstract methods. Whilst this is quite simple, PHP 5.5 brings generators, which allow you to create iterators using a single function.

Here’s an example, using PDO again. First, some boiler plate code to set up:

$dbh = new PDO('sqlite::memory:'); 
$dbh->exec("CREATE TABLE users (
	name VARCHAR(255),

$dbh->exec("INSERT INTO users (id, name) VALUES (1, 'tom')");
$dbh->exec("INSERT INTO users (id, name) VALUES (2, 'dick')");
$dbh->exec("INSERT INTO users (id, name) VALUES (3, 'harry')");

$sth = $dbh->prepare("SELECT * FROM users");

If we wanted to, we could now iterate over $sth using foreach like so:

foreach ($sth as $result) {

Instead, lets create a generator that fetches rows from $sth, but passes control back to the foreach:

function fetch($sth, $fetchMode) {
    while ($result = $sth->fetch($fetchMode)) {
        yield $result;

The key here is the “yield” keyword, which can be thought of as “start returning an item from an array”. The foreach can then loop through the results like this:

$results = fetch($sth, PDO::FETCH_OBJ);
foreach ($results as $result) {
	echo $result->name . "\n";

Whilst this example is a bit pointless (as PDO already provides an Interator), you can see that generators are a simple way of creating iterators.

Enabling and fixing suPHP on Ubuntu

I use suPHP on the VPS that runs this blog as it’s great for performance and security. After updating Ubuntu, suPHP somehow got disabled. Fixing it was a bit of trial and error but in the end this is what worked:

  1. Make sure everything is installed
    sudo apt-get install libapache2-mod-suphp
  2. Edit the end of /etc/suphp/suphp.conf
    ;Handler for php-scripts
  3. Edit /etc/apache2/apache2.conf
    suPHP_Engine on
    suPHP_AddHandler application/x-httpd-php .php
  4. Make sure suPHP is enabled and restart Apache
    sudo a2enmod suphp
    sudo service apache2 restart

That should be it.