Introduction to PHP 7.2 new features

Source: Internet
Author: User
Tags benchmark commas in list hhvm wordpress php

This article to share the content is about PHP 7.2 new features introduced, has a certain reference value, the need for friends can refer to

PHP 7.2 was officially published on November 30, 2017. This release contains new features, features, and enhancements that let us write better codes. In this article, I'll introduce some of the most interesting language features of PHP 7.2.

You can view the full list of more activities on the requests for Comments page.

Core improvements

Parameter type declaration

From PHP5 onwards, we can specify the expected declaration type of the function parameter. If the argument type is wrong, PHP throws an error.

A parameter type declaration (also called a type hint) specifies the type of argument that is expected to be passed to a function or class method.

Here's an example:

Class MyClass {public    $var = ' Hello world ';} $myclass = new myclass;function Test (MyClass $myclass) {    return $myclass->var;} echo Test ($myclass);

In this code, the test function requires a MyClass instance. An incorrect parameter data type can cause a fatal error.

Fatal error:uncaught typeerror:argument 1 passed to test () must is an instance of MyClass, string given, called in/app/ index.php on line and defined In/app/index.php:8

Hints from PHP 7.2 can be used on object-type data, and this improvement allows common object types to be used as arguments to a function or method. Here's an example:

Class MyClass {public    $var = ';} Class FirstChild extends MyClass {public    $var = ' My name is Jim ';} Class Secondchild extends MyClass {public    $var = ' My name is John ';} $firstchild = new FirstChild; $secondchild = new secondchild;function Test (object $arg) {    return $arg->var;} echo Test ($firstchild); Echo test ($secondchild);

In the example above, we have called two test functions, passing a different object each time. This is unprecedented in previous versions of PHP.

Test the type hints for PHP 7.0 and PHP 7.2 in Docker.

Object return type declaration

If the variable type specifies the expected type of the function parameter, the return value type can also be specified as the expected type.

The return type declaration specifies the expected type that a function should return.

From PHP 7.2, the object data type can use the return type declaration. Here's an example:

Class MyClass {public    $var = ' Hello world ';} $myclass = new myclass;function Test (MyClass $arg): Object {    return $arg;} echo Test ($myclass)->var;

The previous PHP version throws the following fatal error:

Fatal error:uncaught Typeerror:return value of test () must is an instance of object, instance of MyClass returned In/ap P/index.php:10

Of course, PHP 7.2 's code will print out ' Hello world '.

Parameter type generalization

PHP is currently not allowed to have any differences between the subclass and the parameter types of its parent class or interface. What does that mean?
Refer to the following code:

<?phpclass MyClass {public    function myFunction (array $myarray) {/* ... */}}class Mychildclass extends MyClass { C3/>public function MyFunction ($myarray) {/* ... */}}

Here we omit the parameter types from the subclass. In PHP 7.0, the following warning is generated:

Warning:declaration of Mychildclass::myfunction ($myarray) should is compatible with myclass::myfunction (array $myarray ) in%s on line 8

From PHP 7.2, we can ignore the type in the subclass without breaking any code. This scenario allows us to upgrade the class in the library so that the type hint can be used without having to update all of its subclasses.

Trailing commas in list syntax

Using a trailing comma on the last element of the PHP array is a legitimate syntax, and sometimes encouraged to do so, it is easy to avoid the error of missing commas when adding new elements. From PHP 7.2 In the grouping namespace, we can use trailing commas.

See trailing commas in list syntax for visual perception of RFC and some sample code.

Security improvements

Argon2 in the cipher hash

Argon2 is a powerful hashing algorithm that won the championship in the 2015 password hashing algorithm, PHP 7.2 as a replacement for the secure Bcrypt algorithm.
The Password_argon2i constants are introduced in the new version of PHP and can now be used in the Password_* series functions:

Password_hash (' password ', password_argon2i);

Unlike Bcrypt, which uses only one cost factor, Argon2 uses three cost factors to differentiate between the following:

    • Defines the memory overhead of the number of KiB that should be consumed during a hash calculation (default is 1 << 10 or KiB or 1 MiB)

    • Defines the time overhead for the hashing algorithm iteration number (default is 2)

    • Parallel factor, the number of parallel threads used to set the hash calculation (default is 2)

The following three new constants define the default cost factor:

    • Password_argon2_default_memory_cost

    • Password_argon2_default_time_cost

    • Password_argon2_default_threads

Here's an example:

$options = [' memory_cost ' + 1<<11, ' time_cost ' and ' 4, ' threads ' = 2];p assword_hash (' Password ', password_a Rgon2i, $options);

Read more about the Argon2 password hash.

Libsodium as part of the PHP core

Starting with version 7.2, PHP covers the Sodium library in its core. Libsodium is a cross-platform and cross-language library for encryption, decryption, signing, password hashing, and more.
This library was previously provided through PECL.
For a list of libsodium functions, see QuickStart.
See also PHP 7.2: The first programming language to add modern encryption technology to its standard library.

Deprecated

Here's a PHP 7.2 deprecated function and feature list, which will be removed after PHP 8.0.

Functions in PHP 5.1 __autoload have been replaced by Spl_autoload_register. A deprecation notification is now reported during compilation.

When a fatal error is thrown, a $php_errormsg local variable is created. PHP 7.2 Should replace this practice with Error_get_last and error_clear_last.

create_function()You can create a function that has a function name, passing the function arguments and the body of the function as a list of the functions. Because of security issues and poor performance, it is marked as deprecated and is encouraged to use encapsulation instead.

mbstring.func_overloadINI set to a value other than 0 has been marked as deprecated.

(unset) castis an expression that always returns null and is useless.

If passed in the second argument, Parse_str () parses the query string into the array, otherwise resolves to a local symbol table. For security reasons, it is not recommended to dynamically set variables in the scope of a function, and using PARSE_STR () without the second parameter will throw a discard notification.

gmp_random()is platform-related and will be discarded. Use Gmp_random_bits () and Gmp_random_rage () instead.

each()Iterations on arrays behave much like foreach (), but foreach () is a better choice for some reason, such as 10 times times faster. Using the former in the loop now throws an discard hint.

The assert() function checks the given assertion and handles it when the result is FALSE. The string parameter is assert() now deprecated because it has an RCE vulnerability. The zend.assertion INI option closes an assertion expression.

$errcontextis an array of local variables that contains the error when it was generated. It can be used as the last parameter of the error handler Set_error_handler () function.

What does PHP 7.2 mean to WordPress users?

According to the official WordPress statistics page, as of this writing, only 19.8% of WordPress users upgraded to PHP 7. Only 5% uses PHP 7.1. You can see that more than 40% of users still use PHP 5.6, and more frightening is that more than 39% of users are using PHP versions that are already unsupported. As of December 2016, WordPress.org has modified the official recommendations for PHP 5.6 users to use PHP 7 or later for the recommended version.

WordPress PHP 7.1 Data statistics

The above data performance is not pleasant, because it looks like PHP 7 seems faster. Here are some statistics:

    • The official PHP benchmark shows that PHP 7 allows the system to execute 2 requests per second, compared to PHP 5.6, which is almost a general delay.

    • Christian Vigh also released a PHP performance test comparison he found that PHP 5.2 was nearly 400% slower than PHP 7.

We ran a performance benchmark in 2018 PHP 5.6 vs PHP 7 vs HHVM. Similar to the benchmark above, we found that PHP 7.2 can perform almost three times times as many transactions per second (requests) as PHP 5.6.

WordPress Benchmark Test

    • WordPress 4.9.4 PHP 5.6 Benchmark results: 49.18 req/sec

    • WordPress 4.9.4 PHP 7.0 Benchmark results: 133.55 req/sec

    • WordPress 4.9.4 PHP 7.1 Benchmark results: 134.24 req/sec

    • WordPress 4.9.4 PHP 7.2 Benchmark results: 148.80 req/sec

    • WordPress 4.9.4 HHVM Benchmark test Result: 144.76 req/sec

Many things are slow to update just because it takes time to participate in testing all the new third-party plugins and themes to make sure they work. Many times, it's slow because they're not finished yet. Not sure what version of PHP you are running? One of the easiest ways to do this is to use the tool Pingdom or Google chrome development tools. The first HTTP request header will typically show your version.

Check the PHP version

This will depend on the value that the host does not modify the X-powered-by header information. If modified, you may not see the PHP version information, in this case you need to upload files via FTP. Or you're always going to ask the host.

Upgrade to PHP 7.2

PHP 7.2 is still a part of the unfinished, but you can start by tasting the early adopters. You can test your WordPress local site or check your scripts in a Docker-like environment, and you can test and compare different versions of PHP on the command line.

Conclusion

Ready to switch to PHP 7.2? But at least I hope you have transitioned to PHP 7 or more in the first place. If you're not ready to test now, upgrade your script, check your code, and say your first experience with PHP 7.2.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.