PHP 7

PHP Göteborg, first meetup, 2015-05-27

Rickard Bennison

What's with the name?

Where did PHP 6 go?

New features

<=>

Combined Comparison Operator (Spaceship)

echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

Null coalesce operator

$username = $_GET['user'] ?? 'nobody';
equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

Grouped use statements

use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo };
compared to:

use FooLibrary\Bar\Baz\ClassA;
use FooLibrary\Bar\Baz\ClassB;
use FooLibrary\Bar\Baz\ClassC;
use FooLibrary\Bar\Baz\ClassD as Fizbo;
            

Engine exceptions

PHP 5.x
function call_method($obj) {
    $obj->method();
}

call_method(null);

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

Engine exceptions

PHP 7
try {
    call_method(null); // oops!
} catch (EngineException $e) {
    echo "Exception: {$e->getMessage()}\n";
}

// Exception: Call to a member function method() on a non-object

Anonymous classes

$pusher->setLogger(new class {
    public function log($msg) {
        print_r($msg . "\n");
    }
});

Closure::call

class Foo { private $x = 3; }
$foo = new Foo;

$foobar = function () { var_dump($this->x); };
$foobar->call($foo); // prints int(3)
equivalent to:

$bound = $foobar->bindTo($foo, $foo);
$bound(); // prints int(3)

Performance

Performance comparison

Requests per second

Source: Zend infographic

Performance

Reduced memory consumption

Redured memory management overhead

Researched JIT compilation

Extensions need to be updated to support phpng

Scalar type hints

int, float, string and bool can now be typehinted

Scalar type hints

By default non-strict, can be declared strict per file.
<?php

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

add("1", "2"); // float(3)

Scalar type hints

By default non-strict, can be declared strict per file.
<?php

declare(strict_types=1);

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

add("1", "2");

// Catchable fatal error: Argument 1 passed to add() must be of the type integer

Return type declarations

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

vs. Hack

Comparable performance

Some similar features

Much smaller step/change

(don't know much about Hack, sorry!)

Backwards Compatibility

"Uniform Variable Syntax"

All deprecated functionality removed

  • ext/ereg (deprecated in 5.3)
  • ext/mysql (deprecated in 5.5)
  • PHP4 constructors
  • ...and more

Timeline

First release candidate (RC) appearing in mid June.

Scheduled release Mid October 2015

Try PHP 7 today

https://github.com/rlerdorf/php7dev

@rickard2

https://about.me/rickard2
www.montania.se