Example PHP7.1 and 7.2 new features

Source: Internet
Author: User
Tags php class

php7.1 new Features

1. Nullable (Nullable) type

The type declaration of the parameter and the return value can be ? marked as empty (null) by adding a question mark () before the type name. Indicates that the type of the function parameter or return value is either a specified type or null.

Take a look at the following example:

function Testreturn (? string $name) {    return $name;} Var_dump (Testreturn (' Yangyi ')); Var_dump (Testreturn (null)); Var_dump (testReturn2 ());

Print output:

$ php php71.phpstring (6) "Yangyi" nullphp Fatal error:  uncaught argumentcounterror:too few arguments to function testr Eturn (), 0 passed in php71.php on line and exactly 1 expected in Php71.php:14stack trace: #0 php71.php (+): Testreturn () #1 {main}  thrown in php71.php on line 14

As above: The third one reported a fatal error.

Again, the function return value is Nullable the case:

function TestReturn3 (): string{    //return "abc";    return null;} Var_dump (TestReturn3 ());

Returns either a string or null if added. ? You can't return anything. Will error.

2.void return type

PHP7.0 adds an attribute that specifies the return type of a function, but the return type cannot be specified as the void,7.1 feature. A function that defines a return type void cannot have a return value, even if it returns null:

function TestReturn4 (): void{    //1. Either return OK    //2. or just return; OK    //return;    3. Return null will also error    //return null;    4. Return 4 will error    //return 4;}
Fatal error:a void function must not return A value in/php71.php on line 70

Also, void can only be used for return values and cannot be used in parameters. For example, the following will be an error:

function testReturn6 (void $a): Void{}var_dump (TestReturn6 ());
PHP Fatal Error:  Void cannot is used as a parameter type in php71.php on line 73

If, in the inheritance of the class, the declaration is a method of the void return type, the subclass inherits the override and can return void only, otherwise it will trigger an error:

<?php class foo{public    function bar (): void {    }}class Foobar extends foo{    /overwrite failed public    function bar ( ): Array {         //Fatal error:declaration of Foobar::bar () must is compatible with Foo::bar (): Void    }}

So, you have to do this, you won't get an error:

Class foo{public    $a;    Public Function bar (): void {        $this->a = 2;    }} Class Foobar extends foo{    //Overwrite success public    function bar (): void {        $this->a = 3;    }}

3.list square brackets ( [] ) and add the specified key

You can use the list to quickly iterate through the values inside the array. Now it's time to use [] shorthand.

$data = [    [1, ' Tom '],    [2, ' Fred '],];//list () stylelist ($id 1, $name 1) = $data [0];//[] style[$id 1, $name 1] = $data  [0];//list () Styleforeach ($data as List ($id, $name)) {    //logic here with $id and $name}//[] Styleforeach ($data as [$id, $name]) {    //logic here with $id and $name}

In addition, the updated list, for the index array, can also specify key, this upgrade is very good, very convenient.

$data = [    ["id" + 1, "name" = "Tom"],    ["id" = + 2, "name" = "Fred"],];//list () stylelist ("id" =  $id 1, "name" + $name 1) = $data [0];//[] style["id" + $id 1, "name" + $name 1] = $data [0];//list () Styleforeach ($data as List ("id" + $id, "name" and "= $name)") {    //logic here with $id and $name}//[] Styleforeach ($data as ["id" = = $id, "name" = + $name]) {    //logic H ere with $id and $name}

Before this feature is available, we typically use while the + each method to list iterate through an indexed array:

$data = [    ["id" + 1, "name" = "Tom"],    ["id" = + 2, "name" + ' Fred '],];while (list ($id, name) = each ( $data) {    echo "$key + $val \ n";}

Note: The Each function has been removed from PHP 7.2! So, don't go through the index array this way.

3. Class constant visibility range setting

In the preceding class, the denomination constants const are declared with no visible attributes. Now you have ported the visible properties of the method:

<?php class Constdemo {    //Changshime considered public    const PUBLIC_CONST = 0;    The visible range of the constants can be customized public    const PUBLIC_CONST_B = 2;    protected const PROTECTED_CONST = 3;        Private Const PRIVATE_CONST = 4;    Multiple constants declare that only one property is    Private Const FOO = 1, BAR = 2;}

Use methods and classes the same way. It's not much more detailed.

4. Support for negative string offsets

There are 2 updates, 1 is a string fetch, and 2 is the third parameter of the STRPOS function that supports negative numbers. Represents the trailing from the tail.

Var_dump ("abcdef" [-2]); Evar_dump (Strpos ("AABBCC", "B",-3)); 3

The string variable can be directly taken, not through the variable name, and is added to the php5.5. Can now be taken from the tail:

Var_dump ("abcdef" [-2]); Take the 2nd character from the bottom: Evar_dump ("abcdef" [2]); Take 2nd from the front, starting from 0: c$string = ' bar '; echo $string [1], $string [-1]; Ar

5. Multi-Conditional catch

In the previous try ... catch statement, each catch can only be set to one condition:

try {    //Some code ...} catch (ExceptionType1 $e) {    //process ExceptionType1} catch (ExceptionType2 $e) {    //processing EXC EptionType2} catch (Exception $e) {    //...}

You can now work with more than one. Split with " | ".

try {    //Some code ...} catch (ExceptionType1 | ExceptionType2 $e) {    //for ExceptionType1 and ExceptionType2 processing} catch (Exception $e) {    //...}

php7.2

PHP 7.2 is mostly a bottom-up update that improves performance. There's not much of a syntax-level update, which is skipped.

Previous versions of php7.0 and older versions of PHP have been written with new points and new features. Look at the new features of php7.1 and php7.2 today.

php7.1 new Features

1. Nullable (Nullable) type

The type declaration of the parameter and the return value can be ? marked as empty (null) by adding a question mark () before the type name. Indicates that the type of the function parameter or return value is either a specified type or null.

Take a look at the following example:

function Testreturn (? string $name) {    return $name;} Var_dump (Testreturn (' Yangyi ')); Var_dump (Testreturn (null)); Var_dump (testReturn2 ());

Print output:

$ php php71.phpstring (6) "Yangyi" nullphp Fatal error:  uncaught argumentcounterror:too few arguments to function testr Eturn (), 0 passed in php71.php on line and exactly 1 expected in Php71.php:14stack trace: #0 php71.php (+): Testreturn () #1 {main}  thrown in php71.php on line 14

As above: The third one reported a fatal error.

Again, the function return value is Nullable the case:

function TestReturn3 (): string{    //return "abc";    return null;} Var_dump (TestReturn3 ());

Returns either a string or null if added. ? You can't return anything. Will error.

2.void return type

PHP7.0 adds an attribute that specifies the return type of a function, but the return type cannot be specified as the void,7.1 feature. A function that defines a return type void cannot have a return value, even if it returns null:

function TestReturn4 (): void{    //1. Either return OK    //2. or just return; OK    //return;    3. Return null will also error    //return null;    4. Return 4 will error    //return 4;}
Fatal error:a void function must not return A value in/php71.php on line 70

Also, void can only be used for return values and cannot be used in parameters. For example, the following will be an error:

function testReturn6 (void $a): Void{}var_dump (TestReturn6 ());
PHP Fatal Error:  Void cannot is used as a parameter type in php71.php on line 73

If, in the inheritance of the class, the declaration is a method of the void return type, the subclass inherits the override and can return void only, otherwise it will trigger an error:

<?php class foo{public    function bar (): void {    }}class Foobar extends foo{    /overwrite failed public    function bar ( ): Array {         //Fatal error:declaration of Foobar::bar () must is compatible with Foo::bar (): Void    }}

So, you have to do this, you won't get an error:

Class foo{public    $a;    Public Function bar (): void {        $this->a = 2;    }} Class Foobar extends foo{    //Overwrite success public    function bar (): void {        $this->a = 3;    }}

3.list square brackets ( [] ) and add the specified key

You can use the list to quickly iterate through the values inside the array. Now it's time to use [] shorthand.

$data = [    [1, ' Tom '],    [2, ' Fred '],];//list () stylelist ($id 1, $name 1) = $data [0];//[] style[$id 1, $name 1] = $data  [0];//list () Styleforeach ($data as List ($id, $name)) {    //logic here with $id and $name}//[] Styleforeach ($data as [$id, $name]) {    //logic here with $id and $name}

In addition, the updated list, for the index array, can also specify key, this upgrade is very good, very convenient.

$data = [    ["id" + 1, "name" = "Tom"],    ["id" = + 2, "name" = "Fred"],];//list () stylelist ("id" =  $id 1, "name" + $name 1) = $data [0];//[] style["id" + $id 1, "name" + $name 1] = $data [0];//list () Styleforeach ($data as List ("id" + $id, "name" and "= $name)") {    //logic here with $id and $name}//[] Styleforeach ($data as ["id" = = $id, "name" = + $name]) {     //logic H ere with $id and $name}

Before this feature is available, we typically use while the + each method to list iterate through an indexed array:

$data = [    ["id" + 1, "name" = "Tom"],    ["id" = + 2, "name" + ' Fred '],];while (list ($id, name) = each ( $data) {    echo "$key + $val \ n";}

Note: The Each function has been removed from PHP 7.2! So, don't go through the index array this way.

3. Class constant visibility range setting

In the preceding class, the denomination constants const are declared with no visible attributes. Now you have ported the visible properties of the method:

<?php class Constdemo {    //Changshime considered public    const PUBLIC_CONST = 0;    The visible range of the constants can be customized public    const PUBLIC_CONST_B = 2;    protected const PROTECTED_CONST = 3;    Private Const PRIVATE_CONST = 4;    Multiple constants declare that only one property is    Private Const FOO = 1, BAR = 2;}

Use methods and classes the same way. It's not much more detailed.

4. Support for negative string offsets

There are 2 updates, 1 is a string fetch, and 2 is the third parameter of the STRPOS function that supports negative numbers. Represents the trailing from the tail.

Var_dump ("abcdef" [-2]); Evar_dump (Strpos ("AABBCC", "B",-3)); 3

The string variable can be directly taken, not through the variable name, and is added to the php5.5. Can now be taken from the tail:

Var_dump ("abcdef" [-2]); Take the 2nd character from the bottom: Evar_dump ("abcdef" [2]); Take 2nd from the front, starting from 0: c$string = ' bar '; echo $string [1], $string [-1]; Ar

5. Multi-Conditional catch

In the previous try ... catch statement, each catch can only be set to one condition:

try {    //Some code ...} catch (ExceptionType1 $e) {    //process ExceptionType1} catch (ExceptionType2 $e) {    //processing EXC EptionType2} catch (Exception $e) {    //...}

You can now work with more than one. Split with " | ".

try {    //Some code ...} catch (ExceptionType1 | ExceptionType2 $e) {    //for ExceptionType1 and ExceptionType2 processing} catch (Exception $e) {    //...}

php7.2

PHP 7.2 is mostly a bottom-up update that improves performance. There's not much of a syntax-level update, which is skipped.

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.