php7: hello world!

38
Pavel Nikolov PHP7: Hello World!

Upload: pavel-nikolov

Post on 16-Apr-2017

1.181 views

Category:

Software


0 download

TRANSCRIPT

Page 1: PHP7: Hello World!

Pavel Nikolov PHP7: Hello World!

Page 2: PHP7: Hello World!
Page 3: PHP7: Hello World!

Who The Fuck Is This Guy?

Pavel Nikolov

Page 4: PHP7: Hello World!

Pavel Nikolov @pavkatar

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

Sofia, Bulgaria

Page 5: PHP7: Hello World!

Hello World!

Page 6: PHP7: 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

Page 7: PHP7: Hello World!

PHP7 CHANGES “UNDER THE HOOD”

Page 8: PHP7: Hello World!

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

Page 9: PHP7: Hello World!

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";

Page 10: PHP7: Hello World!

PHP7 CHANGES “UNDER THE HOOD”

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

Page 11: PHP7: Hello World!

PHP7 CHANGES “UNDER THE HOOD”

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

Page 12: PHP7: Hello World!

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() { /* */ }}

Page 13: PHP7: Hello World!

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

Page 14: PHP7: Hello World!

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:

Page 15: PHP7: Hello World!

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

Page 16: PHP7: Hello World!

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

Page 17: PHP7: Hello World!

PHP7 NEW FEATURES

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

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

Page 18: PHP7: Hello World!

PHP7 NEW FEATURES

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

• Return type declarations === type_declarations

Page 19: PHP7: Hello World!

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';

Page 20: PHP7: Hello World!

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 <=>

Page 21: PHP7: Hello World!

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"?>

Page 22: PHP7: Hello World!

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

Page 23: PHP7: Hello World!

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"]);

Page 24: PHP7: Hello World!

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

Page 25: PHP7: Hello World!

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

Page 26: PHP7: Hello World!

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

Page 27: PHP7: Hello World!

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

Page 28: PHP7: Hello World!

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

Page 29: PHP7: Hello World!

backward compatibility

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

Page 30: PHP7: Hello World!

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']()

Page 31: PHP7: Hello World!

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)

Page 32: PHP7: Hello World!

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;}?>

Page 33: PHP7: Hello World!

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

Page 34: PHP7: Hello World!

$HTTP_RAW_POST_DATA removed

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

Page 35: PHP7: Hello World!

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

Page 36: PHP7: Hello World!

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

Page 37: PHP7: Hello World!

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

Page 38: PHP7: Hello World!

TALK/SMOKETIME