December 2011
PHP 5.4 is getting pretty close to final release and there are quite a number of great new features to look forward to.
Let’s take a quick look..
Short array syntax$items = ['a', 'b', 'c'];
This single improvement is the biggest deal for me. I’m so sick of typing array(..) all the freaking time. :-S
This alone is enough reason to upgrade. It’s something you take for granted in other languages like Python and Ruby. There’s no going back once you’ve tried it.
Better closure supportPHP 5.4 has improved closure support with better handling of $this within a closure.
As an aside—if you’re unfamiliar with the concept of closures, the idea is that closures are functions that are also objects. Confused? Great! Let’s carry on…
class Adder
{
public $amount = 5;
public function add($arr) {
return array_map(function($n) {
// Can access $this inside closure!
return $n + $this->amount;
}, $arr);
}
}
$h = new Adder();
$h->add([2,6,8]); // => [7, 11, 13]
Also good to know—you can manually rebind a closure object’s $this value using Closure::bindTo(..).
If you want to see a a more advanced (crazy but cool) example of how far you can push closures to the limits in PHP, take a look at my PHP Object Orientation hack (updated for PHP 5.4) based on my original hack.
TraitsTraits are a brand new feature in PHP 5.4 and the basic idea to help with code re-use. They’re similar to what other languages would call a Mixin.
You can think of them as “Interfaces with implementation”. But I like to think of it as an assisted copy-and-paste.
It’s useful for when you want to add some behaviour to a class without having to use inheritance. If you think in OO terms—inheritance is usually thought of as a “is-a” relationship—traits are more of a “can-do” relationship.
Here’s a demo of using traits to implement the Observer design pattern:
// Observable is something that can be watched
trait Observable {
private $__observers = array();
public function addObserver($observer) {
$this->__observers []= $observer;
}
public function notify() {
foreach ($this->__observers as $o) {
$o->onEvent($this);
}
}
}
// Observer can watch other things
trait Observer {
abstract public function onEvent($subject);
public function observe($observable) {
$observable->addObserver($this);
}
}
class Dog {
use Observer, Observable; // Dogs can watch or be watched
public $name;
public function __construct($name) {
$this->name = $name;
}
public function bark() {
$this->notify();
}
public function onEvent($subject) {
echo "$this->name watched $subject->name bark!\n";
}
}
$bob = new Dog('Bob');
$doug = new Dog('Doug');
$doug->observe($bob); // Doug is now watching Bob
$bob->bark(); // Displays: "Doug watched Bob bark!"
As you can hopefully see, this is a very useful and powerful tool.
You can add some pretty sophisticated behaviour to an existing class without having to change its implementation much at all.
It’s also great from a DRY point of view, you can avoid repeating code by factoring common functionality into a trait.
For more on the finer points of traits, check out the RFC.
Closing thoughtsThere’s some great improvements and a number of useful new features. It’s great to see the PHP language get some much needed polish.
Now it’s your turn to go and try this stuff out! Get the source on GitHub and compile it yourself or try out some experimental Ubuntu packages.
Possibly the best thing ever.
I’m not typically the stressful type, I usually pride myself on being eternally optimistic and upbeat—but I’m definitely feeling a bit low at the moment. :-(
Maybe it’s the sleep deprivation from my AI and ML classes or maybe it’s the pressure I’m under at work.
Whatever it is—this song is exactly what I needed right now:
Whoa.. there’s 800 people working at Twitter?
I won’t lie.. this is awfully good fun.
“If every additional user is putting money in the developers’ pockets, then you’re less likely to see the site disappear overnight. If every new user is costing the developers money, and the site is really taking off, then get ready to read about those synergies.”
More on Hacker News.