The Evolution of PHP - 5.6 to 8.0 (Part 2)

The translation of the article was prepared in anticipation of the start of the course  "Backend developer in PHP"

(Read the first part)


PHP 7.2

Parameter type extension

<?php

class ArrayClass {
 public function foo(array $foo) { /* … / }
}

//  RFC      , ..        .
//             .
class EverythingClass extends ArrayClass {
 public function foo($foo) { / … / }
}

Counting uncountable objects

A function call count()on a scalar or object that does not implement the Countable interface returns 1 (which is counterintuitive).

count() , , null , Countable.

use  

use Foo\Bar{ Foo, Bar, Baz, };

Argon2

password_* . RFC password_* Argon2i (v1.3) .

PDO

$db = new PDO(…);

//      
$stmt = $db->query('SELECT 1');
var_dump($stmt->activeQueryString()); // => string(8) "SELECT 1"

$stmt = $db->prepare('SELECT :string');
$stmt->bindValue(':string', 'foo');

//      
var_dump($stmt->activeQueryString()); // => string(14) "SELECT :string"

//     
$stmt->execute();
var_dump($stmt->activeQueryString()); // => string(11) "SELECT 'foo'"

PHP 7.3

JSON_THROW_ON_ERROR

JSON , - .

PHP v7.2 , JSON, , ;

:

jsondecode("{");

jsonlasterror() === JSONERRORNONE //  false

jsonlasterrormsg() //  "Syntax error"

, , :

use JsonException;

try {
   $json = json_encode("{", JSON_THROW_ON_ERROR);
   return base64_encode($json);
} catch (JsonException $e) {
   throw new EncryptException('Could not encrypt the data.', 0, $e);
}

, json_encode JSON_THROW_ON_ERROR - :

$e->getMessage(); // jsonlasterrormsg()

$e->getCode(); // jsonlasterror()

iscountable

// :
if (isarray($foo) || $foo instanceof Countable) {
  // $foo  countable
}

// 

if (iscountable($foo)) {
  // $foo  countable
}

arraykeyfirst (), arraykeylast ()

$firstKey = arraykeyfirst($array);
$lastKey = arraykeylast($array);

​​ “samesite” cookie

cookie, samesite: «Lax» () «Strict» (). Lax Strict cookie , HTTP GET. cookie, Lax, GET, , cookie, Strict, GET . POST : cookie POST , .

Set-Cookie: key=value; path=/; domain=example.org; HttpOnly; SameSite=Lax|Strict

PCRE PCRE2

Argon2

password_* . RFC password_* Argon2id Argon2i.

$newArray = array_merge(
   $arrayOne,
   $arrayTwo,
   ['foo', 'bar'],//      
);

list()

$array = [1, 2];
list($a, &$b) = $array;

:

$array = [1, 2];
$a = $array[0];
$b =& $array[1];

PHP 7.4 

class User {
   public int $id;
   public string $name;

   public function __construct(int $id, string $name) {
       $this->id = $id;
       $this->name = $name;
   }
}

FFI (Foreign Function Interface)

FFI - , Python LuaJIT . C C , , « ». PHP FFI PHP C PHP.

Null

//       
$this->request->data['comments']['userid'] = $this->request->data['comments']['userid'] ?? 'value';
//          
$this->request->data['comments']['user_id'] ??= 'value';

PHP (APC, Turck MMCache, Zend OpCache). , PHP . php.ini - opcache.preload. , PHP , . - , opcachecompilefile() .

(ext/hash) , date. , , , .

PHP 8.0

JIT

, PHP , Zend Engine (abstract syntax tree - AST) . - Zend (Zend VM). , , PHP. PHP OPcache .

«JIT» - , , .

PHP, . PHP , . .

typeErrors

, API TypeError, . , ArgumentCountError ( TypeError) , / .

, PHP ( Docker). PHP, .

Macbook pro, Intel Core i7 2,5 .

PHP version : 5.6.40
--------------------------------------
test_math                 : 1.101 sec.
test_stringmanipulation   : 1.144 sec.
test_loops                : 1.736 sec.
test_ifelse               : 1.122 sec.
Mem: 429.4609375 kb Peak mem: 687.65625 kb
--------------------------------------
Total time:               : 5.103

PHP version : 7.0.33
--------------------------------------
test_math                 : 0.344 sec.
test_stringmanipulation   : 0.516 sec.
test_loops                : 0.477 sec.
test_ifelse               : 0.373 sec.
Mem: 421.0859375 kb Peak mem: 422.2109375 kb
--------------------------------------
Total time:               : 1.71

PHP version : 7.1.28
--------------------------------------
test_math                 : 0.389 sec.
test_stringmanipulation   : 0.514 sec.
test_loops                : 0.501 sec.
test_ifelse               : 0.464 sec.
Mem: 420.9375 kb Peak mem: 421.3828125 kb
--------------------------------------
Total time:               : 1.868

PHP version : 7.2.17
--------------------------------------
test_math                 : 0.264 sec.
test_stringmanipulation   : 0.391 sec.
test_loops                : 0.182 sec.
test_ifelse               : 0.252 sec.
Mem: 456.578125 kb Peak mem: 457.0234375 kb
--------------------------------------
Total time:               : 1.089

PHP version : 7.3.4
--------------------------------------
test_math                 : 0.233 sec.
test_stringmanipulation   : 0.317 sec.
test_loops                : 0.171 sec.
test_ifelse               : 0.263 sec.
Mem: 459.953125 kb Peak mem: 460.3984375 kb
--------------------------------------
Total time:               : 0.984

PHP version : 7.4.0-dev
--------------------------------------
test_math                 : 0.212 sec.
test_stringmanipulation   : 0.358 sec.
test_loops                : 0.205 sec.
test_ifelse               : 0.228 sec.
Mem: 459.6640625 kb Peak mem: 460.109375 kb
--------------------------------------
Total time:               : 1.003

, .

meskis/php-bench

PHP 5.6

5.6 servebolt.com. . .

PHP 7.0.0 , PHP . - JIT (Just in time) . PHP 8.0.

PHP 7.x ( ) . PHP .

, :

PHP . , PHP .

TL;DR

, , - PHP 7.3. :

https://wiki.php.net/rfc

https://www.cloudways.com/blog/php-5-6-vs-php-7-symfony-benchmarks/

https://servebolt.com/articles/wordpress-5-0-php-7-2-vs-php-7-3-performance-and-speed-benchmark/


.





All Articles