PHP Version Adoption

PHP runs over 75% of all websites whose technologies are known (source: w3techs), which makes for a really REALLY long tail of users who once installed wordpress, phpmyadmin, or some other open source project that helped their business needs at the time. What they don’t do is upgrade. PHP’s current usage statistics look like this (source and raw numbers are if you want them):

PHP Version Adoption

What’s alarming about this is that the left half of this graph represents unsupported versions of PHP. PHP 5.2 has been end of life since January 2011. This doesn’t mean that you can’t use it any more, but it does mean that in terms of security updates, you are out of luck. Some distributions will try to retro-fit some of the fixes but essentially your PHP applications seem a bit lacklustre because, well, you’re using technology from 2006. Continue reading

Simplest PHP Generator Example

I really like the generators feature that’s arriving in PHP 5.5, and since we’re now into release candidate releases, it’s actually not that far away. I’ve been speaking on this topic and I thought I’d share my trivially simple code example from my slides.

Writing a Generator

The generators use the yield keyword to feed values out as they are iterated over. In code, they really look a lot like a function (or method):


<?php
function getValues() {
// totally trivial example
yield "Apple";
yield "Ball";
yield "Cat";
}

Continue reading

Setting Multiple Headers in a PHP Stream Context

Last week I tried to create a PHP stream context which set multiple headers; an Authorization header and a Content-Type header. All the examples I could find showed headers built up as a string with newlines added manually, which seemed pretty clunky and not-streams-like to me.

In fact, you’ve been able to pass this as an array since PHP 5.2.10, so to set multiple headers in the stream context, I just used this:

<?php
$options = &#91;"http" => [
    "method" => "POST",
    "header" => ["Authorization: token " . $access_token,
        "Content-Type: application/json"],
    "content" => $data
    ]];
$context = stream_context_create($options);

The $access_token had been set elsewhere (in fact I usually put credentials in a separate file and exclude it from source control in an effort not to spread my access credentials further than I mean to!), and $data is already encoded as JSON. For completeness, you can make the POST request like this:



What Goes in Source Control?

Short answer: everything! However we need some good directory structures and source control configuration to make that a really practical answer, so this article is a quick outline of my usual advice for a good source control structure for a standard web project. The examples are for a PHP project but I’m sure you could apply this to your own language of choice, also. Continue reading

The Mathematics of Recruitment and Training

The PHP jobs market is hot, very many people find it hard to recruit the skilled staff that they need to achieve the goals of their organisation. I meet a wide variety of organisations in this technology space, and they all tell me the same story: it’s really difficult to get good, qualified people. And I believe that this is true, I know plenty of developers too and although I’ll usually try to put people in touch where it makes sense to do so, it’s not as if there is a great reservoir of hidden PHP talent somewhere.

This isn’t a rant about salaries, the skills of new graduates, or the trials of dealing with recruiters, although each of those is worth a post in itself. It’s about the mathematics of providing your organisation with the talent it needs at the time that it needs it. Continue reading

Pretty-Printing JSON with Python’s JSON Tool

Today’s quick tip is something that was widely retweeted after my “Debugging HTTP” talk at the ever-fabulous WhiskyWeb conference last weekend. When working with JSON on the commandline, here’s a quick tip for showing the JSON in a nicer format:

curl http://api.joind.in | python -mjson.tool

You need python installed, but the JSON extension is probably included, and that’s all you need for this tool. The result is something like:

python-mjsontool

You can also use this approach to present JSON data that has been captured to another file, for example, it’s a handy trick that I use often when developing something with JSON as a data format.

Video Course on Learnable: OOP PHP

I’m delighted to announce that my new video course on Object-Oriented PHP is now available on Learnable! It’s very much an introduction, aiming to cover WHY objects are so cool as well as how to declare and use one. The course is a mix of video (filmed in my kitchen, welcome to my home everyone!), screencast, a couple of exercises for you to try, and also plenty of sample code to download. If you are just looking to get started with OOP, or know someone who is, then hopefully this will help you out.

On a related note, I’m also doing a Sitepoint “Talk with the Experts” session on 11th April (early morning UK time, as a special treat for everyone in Europe and further east, that doesn’t happen often!). There are more details here: http://www.sitepoint.com/forums/showthread.php?1012242-Talk-Object-oriented-PHP-with-the-Experts and I hope you can join me then.

Ideas Of March: Don’t Read The Comments

It’s that time of year again, time for an “Ideas of March” post (you can read more about this initiative on Chris Shiflett’s blog). Most years many bloggers pledge to write more often, start or restart their blogs, and generally embrace the idea that some thoughts are worth more than 140 characters. Chris himself wrote this year about the demise of google reader, and about blogging as a way of curating and retaining ownership of your ideas, which I thought was an excellent point to make. Continue reading

First Phing Plugin

I’m a huge fan of Phing and use it regularly for build and deployment tasks. Often, I’ll ask about a plugin that I wish existed, and get a very courteous “patches welcome” from the nice people in the channel on freenode. This has happened a few times, so I thought I should probably look at how to make a new phing plugin, this article shows you how to make the simplest thing I could think of: a simple “hello world” plugin. Continue reading

Installing XHGui

There’s a new version of XHGui (well, a few months old) and it’s fabulous! It’s got a few new dependencies though so I thought I’d write down how I set up my version, in case it’s helpful to anyone else (and so I feel like a pro next time I have to do this!). If you’re not familiar with XHGui it’s a fabulously easy and friendly way to profile your application; to understand which method calls in a page take the time and how many times they are made, so you can improve the performance of your application. All these instructions are for my 32-bit Ubuntu 12.10 system, hopefully they will work for you or you’ll be able to adapt them as appropriate.

Dependencies

XHGui needs version numbers or fluffy animal names, because this is a really major release and quite different to what went before in both technology and in looks. In particular, it now uses MongoDB. If you’re not familiar with MongoDB, it’s a super-friendly NoSQL database that makes a really handy backend for this kind of unstructured data – because every run of every page will look different. Therefore you will need:

  • MongoDB itself
  • The pecl extension for mongo
  • The xhprof pecl extension (read on if you’re using PHP 5.4, there’s a gotcha)

Continue reading