PHP 7.2 release has come with a lot of new things that we hardly can ignore to take notice and implementing in our next PHP 7.2 web development project. Here, we have discussed a few important points, in short, to encourage PHP developers to go through the details and code examples to implement in their next projects.

PHP is an open source language with the biggest developer community than any other programming language in the software industry. Therefore, constant enhancements and advancements become apparent with the regular release of the latest versions.

PHP 7.2 – The Best Performing Language

The following graph indicates that how PHP is becoming the best performing dynamic scripting language.

We can see that Perl consumes 5.18.4 seconds to execute, Python 2.7.8, Ruby 2.1 while PHP 7 has the lowest rate 0.281 seconds.

However, PHP 7.0 version is a major release and subsequent minor releases also keeping track of performance augmentation intact and following graph provides solid support for my claim.

It indicates that PHP 7.2 is 20% faster than PHP 7.0 and 10% faster than PHP 7.1. In short, PHP 7.2 is the far better version at algorithm as binary level than its predecessors.

PHP 7.2 – Best Features of the Language

Apart from it, PHP 7.2 also offer revolutionary changes in previous features and introduction of new features. Let’s look all at a glance.

Object Type

It is new scalar type usage of the object type to represent PHP objects. When you run the function test for the object type, you need to input an object parameter ($obj) in the test and get an object value in return. For example,

{
    return new SplQueue();
}

test(new StdClass());

Here we use $obj, a standard class object as input and get the SplQueue object as output. With object type, we add an object as a scalar type of the language. Thus, the object type can fill a previous gap of PHP and proves useful when we are going to manage objects of different classes.

Abstract and Interface Method Override

When an abstract class extends another abstract class, the abstract methods can override. Thus, in an extended abstract method or contravariance, we can avoid the type declaration of a parameter, and we can get a return value or covariance by adding a type hint. Let’s see an example.

{
    abstract function test(string $s);
}
abstract class B extends A
{
    // overridden – still maintaining contravariance for parameters and covariance for return
    abstract function test($s) : int;
}

Here, abstract class A is overriding abstract function the type constraints of string $s. Thus, resulting abstract class B extends A. Similarly, in the case of interface, overriding change the implementation of an interface by removing the type hint of a parameter string $s. in other words, it is parameter type widening.

Argon2

Argon2 is a security algorithm for password hashing and used to secure password storing. PHP 7.2 support Argon2 to offer more security then bcrypt, which is default algorithm used by PHP. The main aim behind the creation of Argon2 is preventing brute force attacks.

The performance of Argon2i is better than Argon2, and its syntax is following.

$argon2i
$v=19
$m=1024,t=2,p=2
$TmxLemFoVnZFaEJuT1NyYg
$4j2ZFDn1fVS70ZExmlJ33rXOinafcBXrp6A6grHEPkI

Description:

Line 1 – Name of the algorithm
Line 2 – Version of the algorithm
Line 3 – Parameters of an algorithm like memory cost (KBs), execution cost (t) and parallelism (p)
Line 4 – A random salt value

Thus, based on the string of given characters like 98 we can verify user password using password_verify() function.

Modern Cryptography Support

One credit goes to PHP 7.2 that it is the first programming language that adds modern cryptography to its standard libraries. With the help of Sodium extension. Sodium uses libsodium library for advanced cryptography including support for ECC.

Added Debug Info for Ext/PDO

For a PDO SQL statement PDOStatement::debugDumpParams function returns debug information, which is very useful for debugging, as it consists of real SQL query sent to databases.
Fortunately, PHP 7.2 accepted the raw SQL string generated by PDOStatement. For example,

$pdo = new PDO(
‘mysql:dbname=test;host=localhost’,
‘user’,
‘password’
);
$sth = $pdo->prepare(“SELECT * FROM user WHERE active=:active”);
if (! $sth->execute([‘active’ => true])) {
print_r($sth->errorInfo());
exit(1);
}
$sth->debugDumpParams();

Now, PHP 7.2 will generate following output:

SQL: [39] SELECT * FROM user WHERE active=:active
Sent SQL: [35] SELECT * FROM user WHERE active=’1′
Params: 1
Key: Name: [7] :active
paramno=-1
name=[7] “:active”
is_param=1
param_type=2

New functions

PHP 7.2 comes with some awesome new functions, such as:

  • ftp_append()
  • hash_hmac_algos()
  • imagesetclip()
  • imagegetclip()
  • imageopenpolygon()
  • imageresolution()
  • imagecreatefrombmp()
  • imagebmp()

Deprecated functions

Similarly, PHP 7.2 has deprecated functions, such as:

  • __autoload()
  • create_function()
  • gmp_random()
  • each()
  • assert() with a string parameter
  • parse_str() without the second parameter
  • png2wbmp()
  • jpeg2wbmp()

Trailing Comma

PHP 7.2 supports trailing comma in multiple arrays and group statements as well as to the list like places, such as grouped namespaces, Function/Method arguments, interface implementation, trait implementation, class member lists, and inheriting variables from parent scope to the anonymous function.

Performance and Speed Comparison among the Latest Versions of PHP
If we look at the performance and speed comparison chart of the latest versions of PHP like 5.2, to 7.0, 7.1, and 7.2 we will find that:

  • PHP 7.2 is 400 times faster than PHP 5.2
  • Compared to PHP 5.6, PHP 7.2 execute nearly three times more transactions per second with the same memory usage.

Conclusion:

PHP 7.2 has introduced several language improvements to make it contemporary and highly accepted web development script. Many offerings are related to PHP security aspects; some are for boosting performance, while rests are covering other aspects of a web development language that is going to be contemporary and grow in future.

Unfortunately, all PHP developers have not acquired PHP 7.2 skills and accustomed to its latest offerings described here.

A selected group of PHP developers has grasped all concepts and implemented in their recent projects, and PHP developers at Perception System are belong to those selected developers.

Would you like to discuss your next PHP 7.2 development project with us?

by Tarang Vyas
Tarang Vyas, CTO at Perception System, a leading PHP Development Company, founded in 2001.

Related Blogs

Comment (0)

Leave a Reply

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