Download - Александр Трищенко: PHP 7 Evolution
![Page 1: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/1.jpg)
PHP 7 EVOLUTION
![Page 2: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/2.jpg)
![Page 3: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/3.jpg)
taken from https://wiki.php.net/phpng
Performance
![Page 4: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/4.jpg)
taken from https://wiki.php.net/phpng
Performance
![Page 5: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/5.jpg)
http://zsuraski.blogspot.com/2014/07/benchmarking-phpng.html
Performance
![Page 6: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/6.jpg)
Removed deprecated functionality
![Page 7: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/9.jpg)
Strict mode isn't strict enough
http://tryshchenko.com/archives/47
![Page 10: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/13.jpg)
Constant arrays using define()
<?php define('ANIMALS', [ 'dog', 'cat', 'bird' ]);
echo ANIMALS[1]; // outputs "cat"
![Page 14: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/17.jpg)
Integer division with intdiv()
var_dump(intdiv(10, 3)); //3
![Page 18: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/18.jpg)
PHP-NG
https://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html
![Page 19: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/20.jpg)
![Page 21: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/21.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/22.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/23.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/24.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/25.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/26.jpg)
ArithmeticError
try
$value = 1 << 1;
catch (ArithmeticError $e)
echo $e->getMessage(), "\n";
![Page 27: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/27.jpg)
DivisionByZeroError
try
$value = 1 << 1;
catch (DivisionByZeroError $e)
echo $e->getMessage(), "\n";
![Page 28: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/28.jpg)
AssertionError
try
$value = 1 % 0;
catch (DivisionByZeroError $e)
echo $e->getMessage(), "\n";
//Fatal error: Uncaught AssertionError: assert($test === 0)
![Page 29: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/29.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/30.jpg)
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 32: Александр Трищенко: PHP 7 Evolution](https://reader036.vdocument.in/reader036/viewer/2022062412/58ec9f161a28abe96a8b458d/html5/thumbnails/32.jpg)