Category Archive: Software

PHP Steps Up Documentation Effort

PHP has some of the best centralised documentation of any programming language. Several years ago they introduced user-contributed comments, which allowed developers a quick way to plug any holes they found. Unfortunately, with the new features introduced in 5.2 / 5.3, as well as more general advances in practices, a lot of these comments are now very dated. Some of them were downright wrong when they were posted.

Now though, PHP.net has made it’s documentation fully editable by members of the public through a web-based DocBook editor at https://edit.php.net. We’re hoping that this will lead to even greater quality of documentation and remove the need for some of the older comments (despite the occasional glaring inaccuracy, most “wiki” benefit hugely from peer review).

The PHP manual has also been released in some additional formats – an extended CHM file with all the user-contributed comments, and a text-based package accessed from the command-line in the same way as Unix man pages (in fact the command is simply “pman”). We’ve wasted no time in adding commands to our various editors to take advantage of this (previously we’d launch a browser window with a keyword search).

Finally, there’s an slightly odd footnote about a future release of the manual in JSON format. We’re not sure what their plan behind this was, but having the manual in an easy-to-use structured format can be no bad thing.

See the news on PHP.net here.

Silex Microframework Installation, Tutorial and Example Program

Silex is a microframework derived from it’s big brother Symfony. It’s described as a simple framework to develop simple websites but as it’s in active development, getting it up and running can be a little tricky. We’re going to do just that and then go through a quick example application to get things moving.

Getting the Source

As Silex draws on part of the Symfony framework, there’s quite a lot of source code to get down and into the right places. It uses git’s submodules system to pull in various other repositories that it requires. If you download our compiled Silex Phar file, you can pick up at the “Sample Application” step.

If you’re running one of the latest versions of git, you can use the “–resursive” option to sort the submodules out, but it’s more likely that you’ll need to take a couple of extra steps:

git clone https://github.com/fabpot/Silex.git
cd Silex
git submodules init
git submodules update

To compile to a Phar file, run the following:

php Silex/compile

If you run into problems at this stage you may need to alter a couple of the configuration options for Phar. Open up your “php.ini” file and make sure the following options are set:

phar.readonly = Off
phar.require_hash = Off

Sample Application

With the compile script run without any problems, we have an archive called “silex.phar” in the folder we were working from. We copy this file into the root of our web application and create an “index.php” to hold our code.

Silex makes it very simple to create a basic application through the use of simple REST-like functions and PHP 5.3′s closures (or anonymous functions). First, we set up Silex by including the autoloader, telling it that we’re using the namespaced Silex\Application class and then instantiating that class:

require 'phar://silex.phar/autoload.php';
use Silex\Application;
$app = new Application();

We can now start to configure the routes of our application, starting with a standard output for “/”, the document root. At the end of the script we call the application’s run method to kick things off.

$app->get('/', function() {
	return "Index Page";
});
$app->run();

Now if we visit “http://localhost/app/index.php”, we’ll see the message “Index Page” in the browser. The real flexibility comes when we use variables in our routing and in our closures. We add this code to our script before the application’s run method.

$app->get('/hello/{name}', function($name) {
	return 'Hello '.$name;
});

By creating a route with part of it surrounded by braces, we annex that part as a variable. This then gets passed into our function so it can be used like any other variable within a function. If we visit our application at “http://localhost/app/index.php/hello/Kryten”, we’ll see the message “Hello Kryten” in the browser.

Futher Reading

We love Silex so we’ll be featuring it a lot over the coming months as it matures. In the meantime, there’s the Silex page on GitHub and slides from this Silex presentation by Igor Wielder. Be careful though as some of the examples used in the presentation are no longer valid.

WordPress Twig Templates

WordPress 3 did a lot to turn it from a blogging platform into a proper CMS and version 3.1 builds on this further with the introduction of post formats. Version 3 also saw the introduction of a new default theme, TwentyTen. The default theme has become bloated, has a complex “parts” system for includes, calls headers and footers separately – overall, it involves too much PHP, too much of the model.

Most frameworks have a templating system that simplifies theme design and obscures as much of the PHP code as possible. WordPress has no such system in place, so we’ve started to develop one for it. We’ve used the Twig templating system, a powerful system with a straightforward syntax.

We’ve started to expose the various parts of the WordPress system through the use of objects and arrays, allowing familiar control structures to be used in themes rather that WordPress’ own system of functions and conditionals. Here’s a simple WordPress loop to run through some posts, echoing links and titles, using the existing WordPress templating:

<?php if(have_posts()): while(have_posts()): the_post() ;?>
    <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
    <p><?php the_excerpt(); ?></p>
<?php endwhile; endif; ?>

Here’s the same loop using Twig templates from within WordPress themes:

{% for post in posts %}
    <a href="{{ post.permalink }}"><h2>{{ post.title }}</h2></a>
    <p>{{ post.excerpt }}</p>
{% endfor %}

Looks a lot simpler doesn’t it? It is. Twig has a huge feature set that can be used in templating, with well though out control structures. See Twig for Designers for a no-nonsense rundown of what it can do.

We’ve recreated most of the WordPress TwentyTen theme using Twig, which is available on GitHub. It’s in it’s early stages and there’s still quite some work to do, but it’s certainly interesting code to look at. We’ve also got an object reference for WordPress templates up on the GitHub wiki to guide you if you are having a play.

Feel free to get in touch via Twitter @acleon with any questions or suggestions.

Download Twig for WordPress from GitHub