PHP5.4 it’s close and it’s time to start playing with the new cool features. I’ve created a new Virtual Machine to play with the new features available within PHP5.4. I wrote a post with the most exciting features (at least for me) when I saw the feature list in the alpha version. Now the Release Candidate is with us, so it’s the time of start playing with them. I also discover really cool features that I pass over in my first review
Let’s start:
Class member access on instantiation.
Cool!
class Human { function __construct($name) { $this->name = $name; } public function hello() { return "Hi " . $this->name; } } // old style $human = new Human("Gonzalo"); echo $human->hello(); // new cool style echo (new Human("Gonzalo"))->hello();
Short array syntax
Yeah!
$a = [1, 2, 3]; print_r($a);
Support for Class::{expr}() syntax
foreach ([new Human("Gonzalo"), new Human("Peter")] as $human) { echo $human->{'hello'}(); }
Indirect method call through array
$f = [new Human("Gonzalo"), 'hello']; echo $f();
Callable typehint
function hi(callable $f) { $f(); } hi([new Human("Gonzalo"), 'hello']);
Traits
trait FlyMutant { public function fly() { return 'I can fly!'; } } class Mutant extends Human { use FlyMutant; } $mutant = new Mutant("Storm"); echo $mutant->fly();
Array dereferencing support
function data() { return ['name' => 'Gonzalo', 'surname' => 'Ayuso']; } echo data()['name'];
IDEs don’t support (yet) those features. That’s means IDEs will mark those new features as syntax errors but I hope that they will support them soon.
More info here
$a = [1, 2, 3];
Finally!
What would the return from “Callable typehint” be?
Gonzalohello or Gonzalo ERROR ?
Ohh, didnt see the
[ ] 🙂
What would
function hi(callable $f) { $f(); }
hi([new Human(), ‘hello’, ‘world’]);
return?
I’m really excited with the sort syntax of arrays 🙂
callable typehint allows us to call “callable” things (closures, function names or even indirect method call through array)
Anyway
hi([new Human(), ‘hello’, ‘world’]);
it’s wrong
array(new Human(), ‘hello’) is a callabel element now (indirect method call through array)
Maybe you want to do
function hi(callable $f, $name) { $f($name); }
hi([new Human(), ‘hello’], ‘world’);
(I don’t have my VM with PHP5.4 so I cannot check it with a real instalation. I will check it later)
Am I the only one who doesn’t like the “echo (new Human(“Gonzalo”))->hello();” syntax?
It would be much better if just
echo new Human(‘foo’)->hello();
worked
echo new Human(‘foo’)->hello();
sounds good but it has conflicts with Human (function) and Human (class).
The only viable solution is (new Human(‘foo’))->hello();
Well you have the
echo new Human;
echo new Human()->foo() = class
echo Human::static = class
echo Human() = function
Human()->foobar
would be a function which returns a class
function Human() {
return new Foo();
}
It would still be ugly oneliner.
I understand It can be “ugly” but IMHO is a cool way to avoid write factory methods. Namespace separator also was ugly at the begininig but now I love it.
Nice! I’ve been waiting a long time for “Array dereferencing support” in PHP (http://codeigniter.com/forums/viewthread/140798/). Love the short array syntax too, as well as any other feature that let us write more concise/terse code.
When will be possible to assign deafult values as in JavasCript ($name = $name || ‘Gonzalo’;)?
You can already assign default values as of PHP 5.3 by using the ternary operator like this:
$name = $name ?: ‘Gonzalo’;
Thanks for the tip! Didn’t know that short-form of the ternary operator. Nice!
It would be nice if was a ‘ternary like’ ruby for this situation:
$name ||= ‘Gonzalo’
(use $name, case blank , assign Gonzalo to it)
Traits look useful. Performance data would be interesting.
I’m still waiting for these:
I’m still waiting for these:
function is_closure($v) {return is_object($v) and is_callable($v);}
function is_iteratable($v) {return is_array($v) or is_object($v) and !is_callable($v);}
Very cool indeed. Traits and new arrays are really interesting:)
this is very cool man, i waiting for this …
$a = [1, 2, 3];
print_r($a);
Its throw Error
Parse error: syntax error, unexpected ‘[‘ in D:\xampp\htdocs\joseph\worex\thephp\array\arrayDe.php on line 2
Remember you need PHP5.4 to run the script