Александр Трищенко: php 7 evolution

32
PHP 7 EVOLUTION

Upload: oleg-poludnenko

Post on 11-Apr-2017

360 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Александр Трищенко: PHP 7 Evolution

PHP 7 EVOLUTION

Page 2: Александр Трищенко: PHP 7 Evolution
Page 3: Александр Трищенко: PHP 7 Evolution

taken from https://wiki.php.net/phpng

Performance

Page 4: Александр Трищенко: PHP 7 Evolution

taken from https://wiki.php.net/phpng

Performance

Page 5: Александр Трищенко: PHP 7 Evolution

http://zsuraski.blogspot.com/2014/07/benchmarking-phpng.html

Performance

Page 6: Александр Трищенко: PHP 7 Evolution

Removed deprecated functionality

Page 7: Александр Трищенко: PHP 7 Evolution

Scalar types declaration

<?php// Coercive modefunction sumOfInts(int ...$ints) return array_sum($ints);

var_dump(sumOfInts(2, '3', 4.1));

//int(9)

Page 8: Александр Трищенко: PHP 7 Evolution

Return type declarations

<?php

function arraysSum(array ...$arrays): array return array_map(function(array $array): int return array_sum($array); , $arrays);

print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

/* Array ( [0] => 6 [1] => 15 [2] => 24 ) */

Page 9: Александр Трищенко: PHP 7 Evolution

Strict mode isn't strict enough

http://tryshchenko.com/archives/47

Page 10: Александр Трищенко: PHP 7 Evolution

Unlike parameter type declarations, the type

checking mode used for return types depends

on the file where the function is defined, not

where the function is called. This is because

returning the wrong type is a problem with the

callee, while passing the wrong type is a

problem with the caller.

Page 11: Александр Трищенко: PHP 7 Evolution

Null coalesce operator

<?php// Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist.

$username = $_GET['user'] ?? 'nobody';

// This is equivalent to:

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

// Coalesces can be chained: this will return the first // defined value out of $_GET['user'], $_POST['user'], and // 'nobody'.

$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';

Page 12: Александр Трищенко: PHP 7 Evolution

Spaceship operator

<?php

// Integers

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

// Floats

echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings

echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1

Page 13: Александр Трищенко: PHP 7 Evolution

Constant arrays using define()

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

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

Page 14: Александр Трищенко: PHP 7 Evolution

Group use declarations

use some\namespace\ClassA, ClassB, ClassC as C; use function some\namespace\fn_a, fn_b, fn_c; use const some\namespace\ConstA, ConstB, ConstC;

Page 15: Александр Трищенко: PHP 7 Evolution

Generator Return Expressions

$gen = (function() yield 1; yield 2;

return 3; )();

foreach ($gen as $val) echo $val, PHP_EOL;

echo $gen->getReturn(), PHP_EOL;

//1 //2 //3

Page 16: Александр Трищенко: PHP 7 Evolution

Generator delegation

function gen() yield 1; yield from gen2();

function gen2() yield 2; yield 3;

foreach (gen() as $val) echo $val, PHP_EOL; /* 1 23 */

Page 17: Александр Трищенко: PHP 7 Evolution

Integer division with intdiv()

var_dump(intdiv(10, 3)); //3

Page 18: Александр Трищенко: PHP 7 Evolution

PHP-NG

https://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html

Page 19: Александр Трищенко: PHP 7 Evolution

New zval structureHashTable size reduced from 72 to 56 bytesSwitch from dlmalloc to something similar to jemallocReduced MM overhead to 5%x64 support

Page 20: Александр Трищенко: PHP 7 Evolution
Page 21: Александр Трищенко: PHP 7 Evolution

Exceptions handling

interface Throwable |- Exception implements Throwable |- ... |- Error implements Throwable |- TypeError extends Error |- ParseError extends Error |- ArithmeticError extends Error |- DivisionByZeroError extends ArithmeticError |- AssertionError extends Error

https://trowski.com/2015/06/24/throwable-exceptions-and-errors-in-php7/

Page 22: Александр Трищенко: PHP 7 Evolution

Exceptions handling

interface Throwable public function getMessage(): string; public function getCode(): int; public function getFile(): string; public function getLine(): int; public function getTrace(): array; public function getTraceAsString(): string; public function getPrevious(): Throwable; public function __toString(): string;

Page 23: Александр Трищенко: PHP 7 Evolution

How to use it now

try // Code that may throw an Exception or Error. catch (Throwable $t)

// Executed only in PHP 7, will not match in PHP 5.x

catch (Exception $e)

// Executed only in PHP 5.x, will not be reached in PHP 7

Page 24: Александр Трищенко: PHP 7 Evolution

Type errors

function add(int $left, int $right) return $left + $right;

try

$value = add('left', 'right');

catch (TypeError $e)

echo $e->getMessage();

// Argument 1 passed to add() must be of the type integer, string given

Page 25: Александр Трищенко: PHP 7 Evolution

ParseError

try

require 'file-with-parse-error.php';

catch (ParseError $e)

echo $e->getMessage(), "\n";

//PHP Parse error: syntax error, unexpected ':', expecting ';' or ''

Page 26: Александр Трищенко: PHP 7 Evolution

ArithmeticError

try

$value = 1 << ­1;

catch (ArithmeticError $e)

echo $e->getMessage(), "\n";

Page 27: Александр Трищенко: PHP 7 Evolution

DivisionByZeroError

try

$value = 1 << ­1;

catch (DivisionByZeroError $e)

echo $e->getMessage(), "\n";

Page 28: Александр Трищенко: PHP 7 Evolution

AssertionError

try

$value = 1 % 0;

catch (DivisionByZeroError $e)

echo $e->getMessage(), "\n";

//Fatal error: Uncaught AssertionError: assert($test === 0)

Page 29: Александр Трищенко: PHP 7 Evolution

Applying function

//PHP5 WAY

PHP

<?phpclass TestClass protected $b = 'hello world';

//function ­ getter

$getB = function() return $this->b; //pay attention, context is undefined now! ;

$applied = $getB->bindTo(new TestClass, 'TestClass');

echo $applied();

Page 30: Александр Трищенко: PHP 7 Evolution

Applying function

//PHP7 WAY

<?phpclass TestClass protected $b = 'hello world'; //function ­ getter $getB = function() return $this->b; //pay attention, context is undefined now! ; echo $getB->call(new TestClass);

Page 31: Александр Трищенко: PHP 7 Evolution

Continue coming soon

Tryshchenko OleksandrDataart 2015

[email protected]:ensaier

Page 32: Александр Трищенко: PHP 7 Evolution