New features in PHP 5.4 alpha 1
Now I’m going to list the new features I like. There’re more features (check the full list here) but those ones are the ones I’m really waiting for:
Added array dereferencing support
When we’re doing OO we can do things like that:
class A
{
public function foo()
{
return new B();
}
}
class B
{
public function bar()
{
return "Hi";
}
}
$a = new A();
echo $a->foo()->bar(); // Will output Hi
But if we output an array instead of an instance of class, we will need an extra variable to use it:
class A
{
public function foo()
{
return array('name' => 'Gonzalo');
}
}
$a = new A();
$foo = $a->foo();
echo $foo['name'];Now with PHP 5.4 we’ll use:
$a = new A(); echo $a->foo()['name'];
Cool, isn’t it?
Added indirect method call through array
Now an array(‘class’, ‘method’) can be used to call a function. is_callable will return true.
That means we can do:
class Foo {
public function bar($name) {
echo "Hi, $name";
}
}
$f = array('Foo','bar');
echo $f('Gonzalo'); // Hi, Gonzalo
More information here
Added closure $this support back
When we use closures in PHP5.3 and we want to use $this statement, we need to do some strange hacks to use it in the context of the callback. Things like $that = $this; and use($that). It works, but looks like an ugly hack. Now we can do:
class A {
private $value = 1;
function single_getter($name) {
return function() use ($name) {
return $this->$name;
};
}
}
class B {
private $value = 2;
function test() {
$a = new A;
$single_getter = $a->single_getter('value');
print $single_getter();
}
}
$b = new B;
$b->test();
PHP5.3 -> Fatal error: Using $this when not in object context
PHP5.4 -> the script will output “1″
More information here.
Added support for Traits
And finally we will enjoy of traits:
Here we can read a really good article about it:
With Traits we can share interfaces and avoid inheritance nightmares to reuse code. Clean and simple.
Conclusions
In my handle opinion those new features will help PHP to grow a little bit more and adapt itself to new years. It’s a pity we still don’t have a short way to define arrays and we still need to use array(1, 2, 3) instead of doing things like [1, 2, 3], but I hope this new feature will be available in future versions I’m not sure but I think it’s on the roadmap.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





