PHP 7.1 new features at a glance

Source: Internet
Author: User
Tags parse error php class php error php foreach

Nullable types are primarily used for parameter type declarations and function return value declarations.

The two main forms are as follows:
<?php
function Answer ():? int {
return null; Ok
}
function Answer ():? int {
Return 42; Ok
}
Function say (? string $msg) {
if ($msg) {
Echo $msg;
}
}

From the example is easy to understand, refers to the passage? Indicates that the type of the function parameter or return value is either a specified type or null.

This method can also be used for the definition of an interface function:
<?php interface Fooable {
function foo (? Fooable $f);
}

But there is one point to note: If the function itself defines a parameter type and does not have a default value, it cannot be omitted, even if it is nullable, or an error is triggered. As follows:
<?php function foo_nullable (? Bar $bar) {}

Foo_nullable (new Bar); Feasible
Foo_nullable (NULL); Feasible
Foo_nullable (); Not feasible

But what if the parameters of the above function are defined as? Bar $bar = null in the form of a third notation is also possible. because = null actually equals? , you can set NULL as the default value for nullable types of parameters.



The square bracket shorthand for list

We know that arrays can only be defined by array () before PHP5.4, and a simplified notation of [] is added after 5.4 (5 characters are omitted).
<?php//5.4 ago
$array = Array (1, 2, 3);
$array = Array ("a" = = 1, "b" = 2, "c" = 3);

5.4 And after
$array = [1, 2, 3];
$array = ["a" = = 1, "b" = 2, "C" and 3];

On the other hand, if we want to assign the value of the array to a different variable, it can be done by using list:
<?php list ($a, $b, $c) = $array;

Is it possible to do so by the shorthand of []?
<?php [$a, $b, $c] = $array;

And the list named key that will be mentioned in the next feature:
<?php ["A" and "a" = $a, "b" = = $b, "c" + = $c] = $array;

The PHP7.1 implements this feature. Note, however, that the [] appearing in the lvalue is not shorthand for the array and is shorthand for list ().


But not only that, the implementation of the new list () can not only appear in the Lvalue, but also in the Foreach Loop:
<?php foreach ($points as ["x" and "= $x," y "and $y]) {
Var_dump ($x, $y);

However, because of the implementation problem, list () and [] cannot be nested with each other:
<?php//illegal
List ([$a, $b], [$c, $d]) = [[1, 2], [3, 4]];

Not legal
[List ($a, $b), List ($c, $d)] = [[1, 2], [3, 4]];

Legal
[[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];



Allow key to be specified in list

As mentioned earlier, the implementation of the new list () can specify key:
<?php $array = ["a" + = 1, "b" = 2, "C" and 3];
["a" + = $a, "b" + = $b, "c" + = $c] = $array;

This is also equivalent to:
<?php $a = $array [' a '];
$b = $array [' B '];
$c = $array [' C '];

The difference between the past and the previous list () is that the key can only be 0, 1, 2, 3, and cannot be adjusted in order. Execute the following statement:
<?php list ($a, $b) = [1 = ' 1 ', 2 = ' 2 '];

Will get PHP error:undefined offset:0 ... The error.

The new implementation can adjust the assignment in the following ways:
<?php List (1 = $a, 2 = $b) = [1 = ' 1 ', 2 = ' 2 '];

Unlike arrays, the list does not support mixed-form keys, and the following notation triggers parsing errors:
<?php//Parse error:syntax error, ...
List ($unkeyed, "key" = = $keyed) = $array;

In more complex cases, the list also supports complex forms of parsing:
<?php $points = [
["X" = 1, "Y" = 2],
["X" = 2, "y" = 1]
];

List (the list ("x" = $x 1, "y" = = $y 1), List ("x" = + $x 2, "y" + = $y 2)) = $points;

$points = [
"First" = [1, 2],
"Second" = [2, 1]
];

List ("First" = List ($x 1, $y 1), "Second" = List ($x 2, $y 2)) = $points;

and use in loops:
<?php $points = [
["X" = 1, "Y" = 2],
["X" = 2, "y" = 1]
];

foreach ($points as List ("x" = $x, "y" = = $y)) {
echo "Point at ($x, $y)", Php_eol;
}


void return type

PHP7.0 adds an attribute that specifies the return type of the function, but the return type cannot be specified as the void,7.1 feature as a supplement:
<?php function should_return_nothing (): void {
return 1; Fatal error:a void function must not return A value
}

The following two scenarios can be verified by:
<?php function Lacks_return (): void {
Valid
}

function returns_nothing (): void {
Return Valid
}

A function that defines a return type of void cannot have a return value, even if it returns null:
<?php function Returns_one (): void {
return 1; Fatal error:a void function must not return A value
}

function Returns_null (): void {
return null; Fatal error:a void function must not return A value
}

Also void applies only to return types and cannot be used for parameter type declarations, or it can trigger an error:
<?php function foobar (void $foo) {//Fatal error:void cannot be used as a parameter type
}

The declaration for a return type in a class function cannot be overridden by a class, or an error is triggered:
<?php class Foo
{
Public Function bar (): void {
}
}

Class Foobar extends Foo
{
Public Function Bar (): array {//Fatal error:declaration of Foobar::bar () must is compatible with Foo::bar (): void
}
}


Class constant property setting

This feature is relatively simple to say that the constants in the class now support the use of public, private, and protected adornments:
<?php class Token {
Changshime thinks public
Const PUBLIC_CONST = 0;

You can customize the visible range of constants
Private Const PRIVATE_CONST = 0;
protected Const PROTECTED_CONST = 0;
Public Const PUBLIC_CONST_TWO = 0;

Multiple constants declare only one property at a time
Private Const FOO = 1, BAR = 2;
}

In addition, constants in the interface (interface) can only be public properties:
<?php interface ICache {
Public Const PUBLIC = 0;
Const IMPLICIT_PUBLIC = 1;
}

In order to cope with the change, the realization of the reflection class is also enriched, adding the Getreflectionconstant and getreflectionconstants two methods to get the extra properties of the constant:
<?php class TestClass {
Const TEST_CONST = ' TEST ';
}

$obj = new Reflectionclass ("TestClass");
$const = $obj->getreflectionconstant ("Test_const");
$consts = $obj->getreflectionconstants ();


Multi-Conditional catch

In the previous try ... catch statement, each catch can only be set to one condition:
<?php try {
Some code ...
} catch (ExceptionType1 $e) {
Handling ExceptionType1
} catch (ExceptionType2 $e) {
Handling ExceptionType2
} catch (Exception $e) {
// ...
}

In a new implementation, you can set multiple conditions in a catch, which is equivalent to or a form of judgment:
<?php try {
Some code ...
} catch (ExceptionType1 | ExceptionType2 $e) {
Handling of ExceptionType1 and ExceptionType2
} catch (Exception $e) {
// ...
}

The handling of exceptions simplifies some.

Manuscripts: Seven Stars interconnection www3.qixoo.com

PHP 7.1 new features at a glance

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.