«

»

Sep 07

Print this Post

Short post #1

Wow, it was almost 4 months since my last post. But don’t worry I’m keeping on mind Marek’s first comment of the first post on this blog ;) I won’t let this blog die :p There are few drafts in my dashboard and with a little fortune I’ll post them in the future :) This post is just a short note to keep “Hello World” alive.

There was a really interesting conference in San Francisco yesterday. Rasmus Lerdorf talked about PHP and its future. Unfortunately there are several hundred kilometers between me and San Francisco and I couldn’t attend this interesting meeting.

Luckily a friend of mine sent me a link to presentation from the conference. It begins with slides about the past and few good points why PHP became so popular (which is somehow connected to topic of one of my drafts mentioned before). Then things Rasmus keeps on mind in 2011. I’m really glad node.js + PHP is mentioned there.

Few slides about performance and I’m sad that I couldn’t listen more about it live. And then slides about PHP 5.3 — I’m shocked: closures in PHP?!

< ?php
$data = array(3,2,8,5,6,1,7,4,9);
usort($data, 
             function($a,$b) { 
                 if($a%2==$b%2) return $a>$b;
                 return ($a%2) ? 1 : -1;
             }
     );
foreach($data as $v) echo "$v ";
?> 
< ?php
$getClosure = function($v) {
    return function() use($v) {
        echo "Hello World: $v!\n";
    };
};

$closure = $getClosure(2);
$closure();
?>

And look at this strange ternary operator use:

< ?php
echo true ?: 'Hello';
echo false ?: 'World';
?>

Finally, after few other features in PHP 5.3 author presents us news from PHP 5.4 world:

  • Short array syntax:
< ?php
$a = [1, 2, 3];
$b = ['foo' => 'orange', 'bar' => 'apple'];
?>
  • FAD which means Function Array Dereferencing:
< ?php
function fruits() {
  return array('apple', 'banana', 'orange');
}
echo fruits()[0]; // Outputs: apple
?>
  • And more about/for closures:
< ?php
class Foo {
  private $prop = "bar";
  public function getPrinter() {
    return function() { echo ucfirst($this->prop); };
  }
}

$a = new Foo;
$func = $a->getPrinter();
$func(); // Outputs: Bar
?>

It recalls me JavaScript so much that I don’t know what to say ;) Is it a good move or bad? One thing which comes to my mind is that with this possibilities and new features PHP programmers should really know the language they use. And not to make mistakes will be more equal to know good patterns.

But there are also more positive changes for me: JSON improvements and UTF-8 as a default encoding.

Feel welcome to visit the slides’ page and to comment!

Permanent link to this article: http://blog.lukaszewski.it/2011/09/07/short-post-1/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>