What's new in PHP

Drupal Camp, Wrocław, 30 May 2015

Michał Pipa

About me

Who is still using PHP 5.2?

Release process

PHP 5.3: end of life

Last release: 5.3.29

PHP 5.4: security fixes only

Released: 1 March 2012

PHP 5.5

Released: 20 June 2013

PHP 5.6

Released: 28 August 2014

PHP 7

Alpha 1: 11 June 2015

Planned release: 12 November 2015

Wait, what about PHP 6?

PHP 6 was abandoned in March 2010

PHP 6 = PHP 5.3 + unicode

unicode = PHP 6 - PHP 5.3

New features

PHP 5.3 features

  • Namespaces
  • Closures
  • Late static binding
  • goto
  • Fixed garbage collection
  • phar

PHP 5.4 features

  • Traits
  • Short array syntax
  • Array dereferencing
  • Class member access on instantiation
  • Class name resolution as scalar via "class" keyword
  • Closures support $this
  • Built-in server

PHP 5.5 features

  • Generators
  • "finally" keyword
  • Simple password hashing
  • Zend Opcache extension

PHP 5.6 features

  • Constant expressions
  • Variadic functions
  • Exponentiation via **
  • use function and use const
  • phpdbg
  • php://input is reusable
  • Large file uploads (> 2GB)
  • SSL/TLS improvements

PHP 5.5: Generators


function getLinesFromFile($fileName) {
    if (!$fileHandle = fopen($fileName, 'r')) {
        return;
    }
 
    while (false !== $line = fgets($fileHandle)) {
        yield $line;
    }
 
    fclose($fileHandle);
}
 
$lines = getLinesFromFile($fileName);
foreach ($lines as $line) {
    // do something with $line
}

PHP 5.5: Simple password hashing

  • md5() and sha1() are unsuitable for passwords
  • password_hash() and password_verify()
  • example output: $2y$12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K
  • Userland implementation for PHP >= 5.3.7

PHP 5.5: "finally" keyword


$db = mysqli_connect();

try {
   call_some_function($db);
} catch (Exception $e) {
   mysqli_close($db);
   throw $e;
}

mysql_close($db);

PHP 5.5: "finally" keyword


$db = mysqli_connect();

try {
   call_some_function($db);
} finally {
   mysqli_close($db);
}

PHP 5.6: Variadic functions


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

PHP 7

PHP 7: Improved performance and memory usage

PHP 7: Anonymous Classes


var_dump(new class($i) {
    public function __construct($i) {
        $this->i = $i;
    }
});

PHP 7: Scalar Type Declarations


<?php
declare(strict_types=1);

function add(int $a, int $b): int {
    return $a + $b;
}

PHP 7: Scalar Type Declarations

  • type checking mode for parameter types depends on the file where the function is called
  • type checking mode for return types depends on the file where the function is defined

PHP 7: Combined Comparison (Spaceship) Operator: <=>

PHP 7: Return Type Declarations


function foo(): array {
    return [];
}

PHP 7: Uniform Variable Syntax

PHP 7: Combined Comparison (Spaceship) Operator: <=>

PHP 7: Null Coalesce Operator: ??


isset($_GET['user']) ? $_GET['user'] : 'nobody'

$_GET['mykey'] ?: ""

$_GET['user'] ?? 'nobody'

PHP 7: Easy User-land CSPRNG


$randomStr = random_bytes($length = 16);
 
$randomInt = random_int($min = 0, $max = 127);

PHP 7: Group Use Declarations


use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\Value;
use Doctrine\Common\Collections\Expr\CompositeExpression;
 
 
use Doctrine\Common\Collections\Expr\{ Comparison, Value, CompositeExpression };

PHP 7: intdiv()


intdiv(int $numerator, int $divisor)

PHP 7: Removed PHP 4 Constructors

PHP 7: Remove alternative PHP tags

  • ASP tags
  • script tags

PHP 7: Remove the date.timezone warning

PHP 7: Exceptions in the engine

PHP 7: Abstract syntax tree

Alternative PHP implementations

HHVM

  • Virtual machine (not a compiler anymore)
  • Uses a just-in-time (JIT) compilation
  • Hack language
    • High performance
    • Type annotations
    • Generics
    • Collections
    • Lambdas

Hippyvm

HippyVM is an implementation of the PHP language using PyPy technology

3v4l.org

Online PHP & HHVM shell, execute code in 100+ different versions!

PHP specification

Thank you

Questions?