New Features and Updates in PHP 7.3 : A Look at Initial RFC

By: Array99      Posted On: 16 Oct, 2018      Category: PHP

The current RC 1 version of PHP has been released and PHP 7.3 is knocking with new useful features, functionalities, deprecations and a good number of bug fixes. In the series of these updates, PHP recently has got another milestone with the release of RC1 of PHP 7.3. You can download the current PHP 7.3 version for your development and testing server, but be remember, this shouldn’t currently be used for production environments. Below, there are listed some new proposed features and updates of PHP 7.3 that are making PHP 7.3 performance much more optimized than the previous versions.

  • Flexible Heredoc and Nowdoc Syntaxes
  • Allow a trailing comma in function calls
  • JSON_THROW_ON_ERROR 
  • PCRE2 Migration
  • list() Reference Assignment
  • is_countable function 
  • array_key_first(), array_key_last() 
  • Argon2 Password Hash Enhancements 
  • Deprecate and Remove image2wbmp()
  • Deprecate and Remove Case-Insensitive Constants 
  • Deprecations for PHP 7.3 
  • Same Site Cookie

Flexible Heredoc and Nowdoc Syntaxes 

Heredoc and Nowdoc syntaxes help out the developers in using multi-line long strings with indentation. The purpose of this to put two changes to the current heredoc and nowdoc syntaxes.

  • To enable for the closing marker to be indented, and
  • To remove the new line requirement after the closing marker

Now, lets take below example:

The indentation of the closing marker will change code from:

<?php
class foo {
    public $bar = <<<EOT
bar
EOT;
}

To

<?php
class foo {
    public $bar = <<<EOT
    bar
    EOT;
}
?>

The indentation of the closing marker dictates the amount of whitespace to strip from each line within the heredoc/nowdoc. Tabs are supported as well, however, tabs and spaces must not be intermixed regarding the indentation of the closing marker and the indentation of the body (up to the closing marker). In any of these cases, a ParseError will be thrown.

Allow a trailing comma in function calls

Allowing a trailing comma in function calls will make it more convenient to append arguments in many contexts where it is common to call a function with lots of arguments; especially variadic functions. Several times we call a number of elements in an array or function call (especially variadic functions), in which if you have missed a comma, you will get an error. For that purpose, trailing commas can be useful there. 

Allowing a trailing comma makes sense in these contexts as new values get appended frequently. There is another context wherein appending more values frequently happens: calling a variadic function.

<?php
unset(
    $foo,
    $bar,
    $baz,
);
?>

Lets take some examples:

Unsetting variables

<?php
unset(
    $foo,
    $bar,
    $baz,
);
?>

Sending variables to a template engine

<?php
echo $twig->render(
    'index.html',
    compact(
        'title',
        'post',
        'comments',
    )
);
?>

Merging arrays

<?php
$newArray = array_merge(
    $array1,
    $array2,
    ['foo', 'bar'],
);
?>

Debugging all the things

<?php
var_dump(
    $bool,
    $string,
    $int,
);
?>

Method & closure calls

<?php
class Player
{
  public function __construct(...$args) {
    //
  }
 
  public function sport(...$args) {
    //
  }
 
  public function __invoke(...$args) {
    //
  }
}

$player = new Player(
  'constructor',
  'sport',
);
 
$foo->sport(
  'method',
  'sport',
);
 
$foo(
  'invoke',
  'sport',
);

?>

Also when in using closures

$players = function(...$args) {
  //
};
 
$players (
  'closure',
  'sport',
);

But this is not allowed in function declaration, means Function declaration syntax will not change. Lets see below syntax that not allowed.

<?php
# Parse error will generate
function bar($a, $b,) {
    //
}
?>

JSON_THROW_ON_ERROR

For parsing JSON responses, we have two functions available json_encode() and json_decode(). Unfortunately, both of them do not have a proper error throwing representation. Json_encode() will throw only false error whereas json_decode will throw null. Only the way to know the error is to call json_last_error() or json_last_error_msg(), which returns the global error state.


This RFC instead proposes adding a new option flag value for json_decode() and json_encode(), JSON_THROW_ON_ERROR. When passed this flag, the error behaviour of these functions is changed. The global error state is left untouched, and if an error occurs that would otherwise set it, these functions instead throw a JsonException with the message and code set to whatever json_last_error() and json_last_error_msg() would otherwise be respectively. 

PCRE2 Migration

The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics. The current version  of it is PCRE2. From PHP 7.3, PCRE2 will come into action for regular expressions in PHP

list() Reference Assignment

PHP has already list() assignment and reference assignment. Under this proposal, a new syntax is introduced:

<?php
$array = [1, 2];
list($a, &$b) = $array;
?>

This would be equivalent to the following:

<?php
$array = [1, 2];
$a = $array[0];
$b =& $array[1];
?>

And also,

<?php
[$a, &$b] = $array;
?>

we can use it with nested list() and skip values as well:

<?php
$array = [1, 2, 3, [3, 4]];
list(&$a, $b,, list(&$c, $d)) = $array;
var_dump($array);
?>

is_countable function 

Currently, we count objects and arrays via count() function. If the object is not countable, PHP throws a warning. So we need to check if the object or variable is countable or not. It has been proposed in PHP 7.3 a new is_countable() function that returns if the passed variable is well countable or not.

<?php
if (is_countable($players)) {
    // $ players is countable
}
?>

Examples:

<?php
var_dump(is_countable([1, 2, 3])); // bool(true)
var_dump(is_countable(new ArrayIterator(['foo', 'bar', 'baz']))); // bool(true)
var_dump(is_countable(new ArrayIterator())); // bool(true)
var_dump(is_countable(new stdClass())); // bool(false)
?>

array_key_first(), array_key_last() 

Arrays is a powerful data structure. In some cases, we have to get the first or the last key of an array. There has been two proposed.
array_key_first()
array_key_last() 
Lets see below example:

<?php

// usage of an associative array
$array = ['a' => 1, 'b' => 2, 'c' => 3];
 
$firstKey = array_key_first($array);
$lastKey = array_key_last($array);
 
assert($firstKey === 'a');
assert($lastKey === 'c');
 
// usage of a numeric array
$array = [1 => 'a', 2 => 'b', 3 => 'c'];
 
$firstKey = array_key_first($array);
$lastKey = array_key_last($array);
 
assert($firstKey === 1);
assert($lastKey === 3);
?>

Argon2 Password Hash Enhancements 

Argon2 Password hashing is a modern algorithm to secure passwords using hashes. It comes in three different types, Argon2i, Argon2d and Argon 2id. Argon2i is optimized for password hashing and password-based key derivation. Argon2d is faster and uses data-depending memory access. Argon2i instead uses data-independent memory access. Argon2id is a hybrid of Argon2i and Argon2d, using a combination of data-depending and data-independent memory accesses. This RFC proposes the implementation of Argon2id within the password_* functions for use as a secure alternative to the originally proposed Argon2i.

<?php
     // Argon2id with default cost factors
     password_hash('password', PASSWORD_ARGON2ID);
?>

 

You may like: