PHP 7

PHP 7

4developers, 11 April 2016

Michał Pipa

About me

PHP 7

phpng

Wait, what about PHP 6?

PHP 6 was abandoned in March 2010

PHP 6 = PHP 5.3 + unicode

unicode = PHP 6 - PHP 5.3

PHP 7 vs. HHVM

Why not HHVM?

  • Deploying is not easy and slow
  • Compatibility
  • Incompatible extensions
  • Developed by single company

Is PHP 7 ready for production use?

Who is using PHP 7?

  • Etsy
  • Badoo

PHP 7: Improved performance and memory usage

PHP 7: Return Type Declarations


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

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: Anonymous Classes


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

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

PHP 7: Uniform Variable Syntax

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: Reserve More Types in PHP 7

PHP 7: Remove alternative PHP tags

  • ASP tags
  • script tags

PHP 7: Remove the date.timezone warning

PHP 7: Exceptions in the engine

Before: Fatal error: Call to a member function method() on a non-object

After: Fatal error: Uncaught exception 'EngineException' with message 'Call to a member function method() on a non-object'

PHP 7: Abstract syntax tree

Tools

  • php7mar
  • Phan
  • Exakat
  • Tuli

3v4l.org

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

PHP specification

Thank you

Questions?