Александр Трищенко: php 7 evolution
TRANSCRIPT
PHP 7 EVOLUTION
taken from https://wiki.php.net/phpng
Performance
taken from https://wiki.php.net/phpng
Performance
http://zsuraski.blogspot.com/2014/07/benchmarking-phpng.html
Performance
Removed deprecated functionality
Scalar types declaration
<?php// Coercive modefunction sumOfInts(int ...$ints) return array_sum($ints);
var_dump(sumOfInts(2, '3', 4.1));
//int(9)
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 ) */
Strict mode isn't strict enough
http://tryshchenko.com/archives/47
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.
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';
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
Constant arrays using define()
<?php define('ANIMALS', [ 'dog', 'cat', 'bird' ]);
echo ANIMALS[1]; // outputs "cat"
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;
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
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 */
Integer division with intdiv()
var_dump(intdiv(10, 3)); //3
PHP-NG
https://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html
New zval structureHashTable size reduced from 72 to 56 bytesSwitch from dlmalloc to something similar to jemallocReduced MM overhead to 5%x64 support
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/
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;
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
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
ParseError
try
require 'file-with-parse-error.php';
catch (ParseError $e)
echo $e->getMessage(), "\n";
//PHP Parse error: syntax error, unexpected ':', expecting ';' or ''
ArithmeticError
try
$value = 1 << 1;
catch (ArithmeticError $e)
echo $e->getMessage(), "\n";
DivisionByZeroError
try
$value = 1 << 1;
catch (DivisionByZeroError $e)
echo $e->getMessage(), "\n";
AssertionError
try
$value = 1 % 0;
catch (DivisionByZeroError $e)
echo $e->getMessage(), "\n";
//Fatal error: Uncaught AssertionError: assert($test === 0)
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();
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);