PHP7: Hello World!

Post on 16-Apr-2017

1,174 views 0 download

transcript

Pavel Nikolov PHP7: Hello World!

Who The Fuck Is This Guy?

Pavel Nikolov

Pavel Nikolov @pavkatar

CEO at ApiHawk Technologies, Geek, API Evangelist. iLover, Developer, Coffee drinker and just an irregular person

Sofia, Bulgaria

Hello World!

Version Release Lifetime

Personal Homepage Tools (PHP tools) v.1 June 8, 1995 ~ 2 years

PHP\FI (Form Interpreter) v2 November 1997 ~ 1 year

PHP3 June 1998 ~ 2 years

PHP4 May 2000 ~ 4 years

PHP5 July 2004 ~ 11 years

PHP5.3 (PHP 6 - Unicode) June 2009

PHP7 Fall 2015

2015 - 20 Years of PHP

PHP7 CHANGES “UNDER THE HOOD”

PHP Version 5.3 5.4 5.5 5.6 7

Interations per second

282544 278205 304330 301689 583975288611 268948 307818 308273 603583288517 279900 296669 301989 606468282384 282060 299921 308735 596079288162 280692 298747 308003 610696286300 278374 307043 303139 594547291027 278703 305950 311487 602184292292 282226 296287 312770 610380

Average 287480 278639 302096 307011 600989

Percentage faster than previous versionN/A -3.08% 8.42% 1.63% 95.76%

Executor: Faster

Executor: Less Memory

32 bit 64 bit

PHP 5.6 7.37 MiB 13.97 MiB

PHP 7.0 3.00 MiB 4.00 MiB

$startMemory = memory_get_usage();$array = range(1, 100000);echo memory_get_usage() - $startMemory, " bytes\n";

PHP7 CHANGES “UNDER THE HOOD”

• Executor: Faster• Executor: Less memory• Compiler: Generates better bytecode

PHP7 CHANGES “UNDER THE HOOD”

• Executor: Faster• Executor: Less memory• Compiler: Generates better bytecode• Parser: Now based on AST (abstract syntax tree)

PHP7 CHANGES “UNDER THE HOOD”

• Executor: Faster• Executor: Less memory• Compiler: Generates better bytecode• Parser: Now based on AST (abstract syntax tree)• Lexer: Now contex-sensitive with support for semi-reserved words

class Collection { public function forEach(callable $callback) { /* */ } public function list() { /* */ }}

PHP7 CHANGES “UNDER THE HOOD”

• Executor: Faster• Executor: Less memory• Compiler: Generates better bytecode• Parser: Now based on AST (abstract syntax tree)• Lexer: Now contex-sensitive with support for semi-reserved words

PHP5.6:PHP Parse error: Syntax error, unexpected T_FOREACH, expecting T_STRING on line 2PHP Parse error: Syntax error, unexpected T_LIST, expecting T_STRING on line 3

callable class trait extends implements static abstract final public protected private const enddeclare endfor endforeach endif endwhile and global goto instanceof insteadof interface namespace new or xor try use var exit list clone include include_once throw array print echo require require_once return else elseif default break continue switch yield function if endswitch finally for foreach declare case do while as catch die self parent

This is a list of currently globally reserved words that will become semi-reservedin case proposed change gets approved:

PHP7 CHANGES “UNDER THE HOOD”

Limitation!

It's still forbidden to define a class constant named as class because of the class name resolution ::class:

In practice, it means that we would drop from 64 to only 1 reserved word that affects only class constant names.

class Foo { const class = 'Foo'; // Fatal error} // Fatal error: Cannot redefine class constant Foo::CLASS as it is reserved in %s on line %d

New Featureshttp://php.net/manual/en/migration70.new-features.php

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

function sumOfInts(int $int, bool $bool, string $string){}

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types=1);

• Return type declarations === type_declarations

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator

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

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=>

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define()

<?phpdefine('ANIMALS', [    'dog',    'cat',    'bird']);

echo ANIMALS[1]; // outputs "cat"?>

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes $var = new class implements Logger {

    public function log(string $msg) {        echo $msg;    }};

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize()

// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]);

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations

use some\namespace\{ClassA, ClassB, ClassC as C};

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations • Integer division with intdiv()

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations • Integer division with intdiv() • Fatal Errors to Exceptions

PHP7 NEW FEATURES• Type declarations: bool, float, int, string

i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations • Integer division with intdiv() • Fatal Errors to Exceptions • Removal of date.timezone Warning

PHP7 NEW FEATURES

• Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types);

• Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations • Integer division with intdiv() • Fatal Errors to Exceptions • Removal of date.timezone Warning • Opcache: opcache.huge_code_pages=1 && opcache.file_cache

backward compatibility

breakshttp://php.net/manual/en/migration70.incompatible.php

Old and new evaluation of indirect expressions

Expression PHP 5 interpretation PHP 7 interpretation

$$foo['bar']['baz'] ${$foo['bar']['baz']} ($$foo)['bar']['baz']

$foo->$bar['baz'] $foo->{$bar['baz']} ($foo->$bar)['baz']

$foo->$bar['baz']() $foo->{$bar['baz']}() ($foo->$bar)['baz']()

Foo::$bar['baz']() Foo::{$bar['baz']}() (Foo::$bar)['baz']()

foreach no longer a the internal array pointer

<?php$array = [0, 1, 2];foreach ($array as &$val) {    var_dump(current($array));}?>

Output of the above example in PHP 5: int(1) int(2) bool(false)

Output of the above example in PHP 7: int(0) int(0) int(0)

foreach by-reference has improved iteration behaviour ¶

Output of the above example in PHP 5: int(0)

Output of the above example in PHP 7: int(0) int(1)

<?php$array = [0];foreach ($array as &$val) {    var_dump($val);    $array[1] = 1;}?>

Changes to Division By Zero

Output of the above example in PHP 5:

Warning: Division by zero in %s on line %dbool(false)

Warning: Division by zero in %s on line %dbool(false)

Warning: Division by zero in %s on line %dbool(false)

<?phpvar_dump(3/0); var_dump(0/0); var_dump(0%0);?>

Output of the above example in PHP 7:

Warning: Division by zero in %s on line %dfloat(INF)

Warning: Division by zero in %s on line %dfloat(NAN)

PHP Fatal error: Uncaught DivisionByZeroError: Modulo by zero in %s line %d

$HTTP_RAW_POST_DATA removed

$HTTP_RAW_POST_DATA is no longer available. The php://input stream should be used instead.

JSON extension replaced with JSOND ¶

The JSON extension has been replaced with JSOND, causing two minor BC breaks.

Firstly, a number must not end in a decimal point (i.e. 34. must be changed to either 34.0 or 34).

Secondly, when using scientific notation, the e exponent must not immediately follow a decimal point (i.e. 3.e3 must be changed to either 3.0e3 or 3e3).

Compatibility Checkhttps://github.com/sstalle/php7cc

https://hub.docker.com/_/php/

TALK/SMOKETIME