A brief introduction to the new characteristics of php7.1

Source: Internet
Author: User


Allow Specifying keys in list ()
Square bracket syntax for array destructuring assignment

The following support effect, or a good

List ($a, $b) = Array ($b, $a);
[$a, $b] = [$b, $a];

foreach ($points as ["X" => $x, "y" => $y]) {
Var_dump ($x, $y);
}
This can be very handy and powerful when traversing.
Feeling these two features makes the array easier to use

Nullable Types

This is a shorthand scalar or null, useful, but not very good.

Generalize support of negative string offsets

This feature is good enough to simplify some simple string processing

Support Class Constant Visibility

For the time being, it's just that the class constants are perfect.

Void return Type

This limit function returns a value that is useful

At the same time, hoping to miexd,number something.

Catching multiple Exception Types

It's not good to have more cacth, but it's probably not very unusual.


The rest is obvious, there's no better discussion, so let's talk about it.


String indexes can use negative numbers

$str = ' abcdef ';
Var_dump ($str [-2]); => string (1) "E"

$STR {3} = '. ';
Var_dump ($STR); => string (6) "Abc.ef"

Var_dump (Isset ($str {4})); => bool (TRUE)

Var_dump (Isset ($str {10})); => bool (FALSE)
Void return type

If a method is set to return a void type, it will not be able to return any value

function foo (): void
{
return 1; Error, cannot have return
}

function foo (): void
{
Return That's right
}

function foo (): void
{
Also correct
}

Note, however, that NULL cannot be returned:

function foo (): void
{
return null; That's not going to work!
}

Finally, if the subclass wants to overwrite a method that returns void for the parent class, the return type cannot be changed.

class constant Visibility setting

Class Token
{
Default is Public property
Const PUBLIC_CONST = 0;

You can add visibility settings
Private Const PRIVATE_CONST = 0;
protected Const PROTECTED_CONST = 0;
Public Const PUBLIC_CONST_TWO = 0;

All constants can only be set once visibility
I can write that.
Private Const FOO = 1, BAR = 2;
}

Multiple Exception simultaneous Catch


try {
Some code ...
catch (ExceptionType1 | ExceptionType2 $e) {
It's useful if you have different exceptions that you want to deal with in one way.
catch (\exception $e) {
// ...
}
Well, the new features of PHP 7.1 are these, do you have any expectations?

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.