Posts filed in “Programming”

The Network as Data Storage

Just like a file, a socket is an ordered stream of data. Do you know what’s also ordered? You’re right, a queue. So the network is essentially a queue. You know any of those fancy job processing systems, like Gearman or RabbitMQ? We can use the network to achieve some of their features, without any infrastructure.

I'm writing a book about socket programming in PHP

Since 2012 lots of people have read this blog post about socket programming. It’s the most read post on this blog, year after year. It even got translated into two other languages than English. Many people are sending me questions by email.

Now I’m writing a little book, to make a more comprehensive look at socket programming in PHP.

The book will launch in Fall 2014. You can subscribe now to my mailing list to get the entire first chapter for free, get early access to the launch, and a code for a 20% discount.

Get your free sample chapter now!

Official Heroku PHP support updated with Composer, HHVM and NGINX support

A long due update to the Heroku PHP buildpack —which adds support for modern PHP development— is now in public beta. Their Devcenter Article has more info.

You know what’s really awesome about it? You can require heroku/heroku-buildpack-php in your Composer dev requirements and run your apps locally with a Procfile and Foreman, just like every other supported language on Heroku.

Congrats David Zülke!

PSA: Once the official PHP buildpack is stable, I plan to no longer support my own PHP buildpack.

On call_user_func()

Little known change of PHP 5.4: you don’t have to use the call_user_func() function anymore to call generic callables!

Before 5.4 you could only use () to call variables when the type was a String or Closure, otherwise you would have to use call_user_func(). Since 5.4 however, you can simply do () on anything which satisfies the callable typehint, as illustrated below:

<?php

class Foo
{
	function bar()
    {
        echo "Hello World\n";
    }
}

$cb = array(new Foo, "bar");
$cb();

$cb = "strlen";
var_dump($cb("foo"));

(Try it)

Currently this doesn’t make much difference, but it’s a nice step to improve readability a bit.

Together with Argument Unpacking (currently slated for 5.6) this could make call_user_func_array() obsolete too.

Yay! We won’t have to use functions to call functions anymore!

How to log to the PHP error log in Symfony 2

Sometimes you want to log to PHP’s error_log in a Symfony 2 app. This is useful if your app runs in a container (think Docker) and PHP’s error log is redirected to standard output and you want your application logs to show up there too.

You need to use at least version 2.4 of the MonologBundle to use the ErrorLogHandler:

{
    "require": {
        
        "symfony/monolog-bundle": "~2.4"
    }
}

Then in your config_prod.yml and config_dev.yml change the type of the Monolog handlers to error_log and you are all set. For example, the Monolog config in config_dev.yml would look like this:

monolog:
    handlers:
        main:
            type:  error_log
            level: debug

Go Patterns

I must admit that I’m a serious fan of Go. I spent most of the last company hackdays with it, doing some small projects, like a simple language independent job queue, and some smaller libraries.

Grails Plugin Etiquette

At work we use a lot of Grails plugins. Recently one of our most important Plugins broke functionality in our app in a minor release, does not tag the releases in the repository and doesn’t provide a upgrading document. This brought me to think a bit about how plugins should behave.