PHP 5.3 finally released

July 3, 2009 | by Jakub Zalas

Comments

Finally it happened: PHP 5.3 was released. It was a long awaited release, as it brings many improvements and features together with over 140 bug fixes. It’s almost like PHP 6 but without full Unicode support. In this post I will present the things I was either particularly waiting for or was just interested in.

PHP 5.3 has some nice new features

Optional Garbage Collection

Up till now PHP was suffering from memory leaks when cyclic references were involved. The problem was really well described by Derick Rethans in Collecting Garbage: PHP’s Take on Variables (article published in April’s issue of php|architect). In PHP 5.3 we have a new familly of garbage collector functions available (gc_*) which allow us to have the control over circular reference collector.

Performance improvements

Release announcement mentions the “under the hood performance improvements”. It’s not specified in much detail but it’s always good to hear ;)

Namespaces

Namespaces in PHP were designed to solve two problems: avoid name collisions and improve readability. First problem reveals itself mainly when external library is used with your own code. It sometimes happens that class or function names are duplicated. Namespaces help to avoid such cases and as a side effect also improve readability. Because namespaces introduce better separation, the class names no longer have to be long to keep them unique.

Ternary Shortcut

It is now possible to leave out middle part of ternary operator. Instead of writing:

echo $test ? $test : 'not true';

We can use a shortcut:

echo $test ?: 'not true';

It can be really useful in templates to create shorter statements.

Lambda Functions and Closures

Closures, also known as anonymous functions, allow you to create functions without a specified name. They are meant to be use-and-throw-away functions and that’s why they are the most useful as a callback parameters.

$values = array_map(function ($value) { return strtoupper($value) }, array('a', 'b', 'c'));

People already came up with many usage examples and some of them look really scary. For sure this is like an axe which can cause a lot of troubles when you put it in wrong hands. It should just be used with care like every powerful feature.

Jump label (limited goto)

Goto is a kind of controversial thing. Why add a function which is despised almost through the whole developers’ world?

goto statement by xkcd

Image source: xkcd.com

phar extension

The extension provides an interesting feature similar to JAR files known from java world. It allows putting the whole application into the phar file (PHP Archive). It’s meant to make distribution process easier.

What’s next?

Migration! There is already a migration guide available. It’s high time to read it and see what has changed. There are some backward incompatible changes which should draw your attention. In the end it’s not a major release and it should go quite smoothly. If you use third party frameworks and libraries the best way to handle migration is wait until PHP 5.3 compatible versions are released.

Feel free to discuss your opinion with us below or through our Twitter account @GOYELLO.

blog comments powered by Disqus