PHP 7.2 New Features

PHP 7.2 New Features

Content :-

  • efault values are 1<<10, or 1024 KiB, or 1 MiB)
  • time cost that defines the number of iterations of the hashing algorithm (defaults to 2)
  • parallelism factor, which sets the number of parallel threads that will be used during hashing (defaults to 2)

Three new constants define default cost factors:

  • PASSWORD_ARGON2_DEFAULT_MEMORY_COST
  • PASSWORD_ARGON2_DEFAULT_TIME_COST
  • PASSWORD_ARGON2_DEFAULT_THREADS

Here is an example:

$options = ['memory_cost' => 1<<11, 'time_cost' => 4, 'threads' => 2];password_hash('password', PASSWORD_ARGON2I, $options);

See Argon2 Password Hash for more information.


2. Parameter Type widening

PHP till version 7.2 wasn’t supporting parameter type variance, which means if we declared a parameter with a specific type and then we omit this type whenever we override this method, PHP is going to squawk due to compatibility.

Let’s give it a shot by the following code sample.

<?php 
class ParentClass{
public function findBy(int $id){
}
}
class ChildClass extends ParentClass{
public function findBy(string $name){
}
}


Warning: Declaration of ChildClass::findBy(string $name) should be compatible with ParentClass::findBy(int $id) in /Projects/index.php on line 11

PHP 7.2 allowed us to omit the type-hinting .So,you can override it or whatever . this won’t cause an error anymore which is a great feature to facilitate coding, You don’t have to restrict yourself with a specific type hint when it comes to overriding .

Compatibility shouldn’t be a concern to you anymore as the type widening fixes that .

3. Object Type Hinting

In The past we couldn’t declare a specific type to return, which is something really annoying . I often needed to restrict the type to an object , But I simply couldn’t, I had to type hint with a specific class.
Thanks to PHP 7.2, we can now type hinting with object.

<?php
class Model {
public $table = 'users';
public function __construct($table = null){
if($table){
$this->table = $table;
}
}
public function getTable(){
return $this->table;
}
}
$myclass = new Model;
function test(Model $arg) : Object {
return $arg;
}
echo test($myclass)->getTable();

If we ever try to type hint with object before PHP 7.2, PHP would consider that as a class and the returned value should be an instance of this class.


4. Trailing comma in list syntax

PHP 7.2 now supports trailing comma in multiline arrays look like the following.

Use Foo\Bar\{

Foo,
Bar,
Baz,
};

Trailing comma is proposed to the all list like places

  • Grouped namespaces
  • Interface implementation
  • Trait implementation
  • Inheriting variables from parent scope to anonymous function


5. New Sodium extension

Starting from version 7.2, PHP includes the Sodium library into the core. Libsodium is a cross-platform and cross-languages library for encryption, decryption, signatures, password hashing and more.
 The library was previously available through PECL.


6. Core Improvements.

  • PHP 7.2 and PHP 5.2 performance comparison says that PHP 7.2 is 400 times faster than PHP 5.2
  • PHP 7.2 could execute almost three times as many transactions per second as compared to PHP 5.6.


7. Deprecation

The __autoload function has been superseded by spl_autoload_register in PHP 5.1. Now a deprecation notice would be thrown when it’s encountered during compilation.

The $php_errormsg variable is created in the local scope when a non-fatal error is thrown. Since PHP 7.2 error_get_last and error_clear_last should be used instead.

each() is used to iterate over an array much like foreach(), but foreach() is preferable for several reasons, including being 10 times faster. Now a deprecation will be thrown on the first call in a loop.

create_function() allows the creation of a function with a generated function name, a list of arguments and body code provided as arguments. Due to security issues and bad performance, it has been marked as deprecated and the usage of enclosures is encouraged instead.

  • (unset) cast is an expression that always returns null and is considered useless.

This one is really consiered useless as whenever you pass to it a variable, neither the variable or the variable’s value will be set to null, It will just return null .

Don’t get twisted, That has nothing with the unset function , the unset function removes the variable and has an effect unlike the unset cast.

Leave a Reply

Your email address will not be published. Required fields are marked *