PHP

From 5.3 to 5.5

Presentation by Hugo Agbonon (@Hugo_Agbonon), created using impress.js

PHP 5.3

Release date: 2009-06-30

It's been a while...

Basis for Symfony 2, Zend Framework 2, Laravel 4...

Current version: 5.3.27

It added loads of things

  • Namespaces
  • Closures
  • Late static binding
  • Nowdoc
  • goto
  • Nested exceptions

End of support: 2014-07

No more bugs are fixed, and in less than a year, no more security fixes!

PHP 5.4

Release date: 2012-03-01

It took almost 4 years!

Current version: 5.4.21

End of support: ~2015

It got rid of...

Main additions:

In addition to a big performance boost
  • Arrays short syntax
  • Array dereferencing
  • $this in closures
  • Class member access on instantiation
  • Traits
  • Webserver
  • Other things

Array short syntax

$array = array('PHP', '5.3');
$array = ['PHP', '5.4+'];

Array & string dereferencing

echo array('PHP', '5.4+')[0];
function helloTF1() {
  return ['Hello', 'TF1'];
}
echo helloTF1()[1];
echo 'Hugo'[1];
class A {
  private $value = 1;
  public function getClosure()
  {
    return function() {
      return $this->value;
    };
  }
}

$a = new A;
$fn = $a->getClosure();
echo $fn(); // 1
Class member access on instantiation has been added, e.g. (new Foo)->bar().

Traits

The main addition
Traits are a mechanism for code reuse in single inheritance languages such as PHP
trait Color {
    public function getColor() {
        return 'blue';
    }
}
class Vehicle {}
class Car extends Vehicle {
    use Color;
}
$myCar = new Car;
echo $myCar->getColor(); // blue

A new keyword, insteadof, has been added to prevent conflicts in traits

Without it, if you get a conflict, fatal error.

Be careful when using traits

Don't use them as a replacement for class inheritance (as seem to do 50% of the explanations on the Internet)

Web server

hugo@VF19:~> php -S localhost:8000
python -m SimpleHTTPServer 8000

Obviously, only for tests

One last thing...

htmlspecialchars & htmlentities's default character set changed from ISO-8859-1 to UTF-8

PHP 5.5

Release date: 2013-06-20

Current version: 5.5.5

End of support: ~2016

It got rid of...

It deprecated

  • The original MySQL extension
  • The /e flag in preg_replace

Main changes:

  • empty()
  • Generators
  • try, catch... finally!
  • Other things

empty

My personal favorite : expressions, finally!
$a = '';
empty($a);
$b = '        ';
empty(trim($b));

Generators (yield)

array range (mixed $start , mixed $end)
function xrange($start, $limit) {
    for ($i = $start; $i <= $limit; $i++) {
        yield $i;
    }
}
$data = range(0, 1000000);
echo memory_get_usage() / 1048576 . " MB";
// output: 137.92 MB
$data = range(0, 1000000);
echo memory_get_usage() / 1048576 . " MB";
// output: 0.22 MB
foreach ($data as $key => $val) {
  echo "key: ".$key." value: ".$val."\n";
}

try... catch... finally!

try {
   //do something
} catch(Exception ex) {
   //handle an error
} finally {
   //clean up after yourself
}

Other things

class A {}
echo A::class;
$array = [[1, 2], [3, 4]];
foreach ($array as list($a, $b)) {
    echo "First: $a; Second: $b\n";
}

Zend Optimizer+ as OPcache extension

New functions for secure password hashing

What happened to JSON?

The Software shall be used for Good, not Evil.
Douglas Crockford
We want to (be able to) be evil!
PHP developers (not really)

So the extension ext/JSON got replaced by a free one and there should* not exist any problems

*Except some distribs (Debian, Fedora) removed the old extension but did not automatically add the new one, so users panicked when json_encode and json_decode crashed

The future

Just in case you were wondering...

No more 4 years gap between two versions

A yearly cycle has started in 2011

2 years with bug & security fixes, 1 year with only security fixes

PHP 5.6

Should be out in 2014

Variadic functions

(accepted)
class MySQL implements DB {
  public function query($query, ...$params) {
    $stmt = $this->pdo->prepare($query);
    $stmt->execute($params);
    return $stmt;
  }
    // ...
}

Skipping optional parameters for functions

(under discussion)
function create_query(
  $where, $order_by, $join_type='',
    $execute = false, $report_errors = true
);
create_query(
  "deleted=0", "name", default,
    default, false);

Problems for the future : the (lack of) vision

The PHP internals are a battlefield

What major changes should PHP 6 bring?

And existing problems still have to be fixed

Look at all the amazing stuff we build - The language can’t be that bad!

I don't believe it is :)

Thank you :)

Any questions?

(I have absolutely no idea if I have time to take some, though)