PHP 7

PHP Göteborg, Meetup #3, 2016-02-25

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 coalescing 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)

Expectations

switch ($type) {
    case "foo": …
    case "bar": …
    default:
        assert(false, 'Unknown type ' . $type);
}
# zero cost for production
zend.assertions = -1 

Session handling improvements

session_start([
    'read_and_close' => true, // Read and then close the session lock
]);
# Only write when session data changes
session.lazy_write = 1 

CSPRNG Functions

string random_bytes ( int $length )

int random_int ( int $min , int $max )

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 declarations

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 [];
}

Complete list

Backwards Compatibility

"Uniform Variable Syntax"

All deprecated functionality removed

Some errors changed to Exceptions

E_STRICT removed and moved to other levels

list() with strings is unsupported

Division by zero returns INF/NAN instead of false

Scalar names can no longer be used as class names

foreach is now less quirky

Backwards Compatibility

- the WTF list

Strings containing hexadecimal numbers are no longer considered to be numeric

Calls from incompatible context removed

Functions cannot have multiple parameters with the same name

Switch statements cannot have multiple default blocks

<script language="php">echo 'no longer works';</script>

Try PHP 7 today

https://www.dotdeb.org/instructions/ https://github.com/rlerdorf/php7dev

PHP 7.1 RFCs

implemented

PHP 7.1 RFCs

pending implementation

PHP 7.1 RFCs

voting now

Under discussion

  • Deprecations for PHP 7.1
  • Traits with interfaces
  • Class Friendship
  • Trailing commas in list syntax
  • Automatic SQL injection protection
  • Introduce consistent function names
  • Introduce Design by Contract
  • Native Design by Contract support as annotation
  • Native Design by Contract support as definition
  • Secure serialization by authentication code
  • Comparable
  • Additional splat operator usage
  • Make PHP open tag optional for better security
  • Named Parameters
  • Adopt Code of Conduct
  • Warn about invalid strings in arithmetic
  • Normalize token_get_all() output
  • On-demand Name Mangling
  • Add array_key_(first|last|index) functions
  • Default Value in List Assignment Syntax
  • Closure from callable function
  • More precise float value handling
  • Improved Error Callback Mechanism
  • Introduce script only require/include
  • Precise URL include control
  • Deprecate INI set/get aliases
  • GitHub Pull Requests Triage Team
  • Change checkdnsrr() $type argument behavior
  • Loosening heredoc/nowdoc scanner
  • Binary String Comparison
  • password_hash function behavior
  • Use php_mt_rand() where php_rand() is used
  • ReflectionParameter Typehint accessors
  • Secure Session Options
  • Unify crypto source INI setting
  • Build OpenSSL module by default
  • Module API introspection
  • Add support for GMP floating point numbers
  • Normalizing increment and decrement operators
  • Precise Session Management
  • Internal Serialize API
  • unset(): return bool if the variable has existed
  • Integrate voting polls in PHP.net
  • Escaping RFC for PHP Core
  • Add is_cacheable() stream-wrapper operation
  • Add cyclic string replace to str_[i]replace() functions (Created 2015/01/05)
  • Add generic class and method support (Created 2015/04/28)
  • Add resource typehint (Created 2015/11/11)
  • Add PHP Engine Identifier Constant (Created 2016/02/03)

@rickard2

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