PHP 7.1 new features at a glance

黄舟
Release: 2023-03-05 12:10:01
Original
1534 people have browsed it

Nullable type

Nullable type is mainly used for parameter type declaration and function return value declaration.

The main two forms are as follows:

<?phpfunction answer(): ?int  {   return null; //ok}function answer(): ?int  {
   return 42; // ok}function say(?string $msg) {
   if ($msg) {
       echo $msg;
   }}
Copy after login

It is easy to understand from the example. What it means is to use the form of ? to indicate 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 interface functions:

<?php interface Fooable {    function foo(?Fooable $f);}
Copy after login

But there is one thing to note: if the function itself defines the parameter type and has no default value, even if it is nullable , cannot be omitted, otherwise an error will be triggered. As follows:

<?php function foo_nullable(?Bar $bar) {} foo_nullable(new Bar); // 可行foo_nullable(null); // 可行foo_nullable(); // 不可行
Copy after login

But if the parameters of the above function are defined in the form of ?Bar $bar = null, the third way of writing is also feasible. Because = null is actually equivalent to a superset of ? , for nullable type parameters, null can be set as the default value.


The square bracket abbreviation of list

We know that before PHP5.4, arrays could only be defined through array(), which was added after 5.4 [] is a simplified way of writing (it is still very practical to omit 5 characters).

<?php // 5.4 之前$array = array(1, 2, 3);$array = array("a" => 1, "b" => 2, "c" => 3); // 5.4 及之后$array = [1, 2, 3];$array = ["a" => 1, "b" => 2, "c" => 3];
Copy after login

Extended to another question, if we want to assign the value of the array to different variables, it can be achieved through list:

<?php list($a, $b, $c) = $array;
Copy after login

Can it also be achieved through the abbreviation of [] Woolen cloth?

<?php [$a, $b, $c] = $array;
Copy after login

And the list specified key mentioned in the next feature:

<?php ["a" => $a, "b" => $b, "c" => $c] = $array;
Copy after login

PHP7.1 implements this feature. But it should be noted that the [] that appears in the lvalue is not the abbreviation of array, but the abbreviation of list().


But that’s not all, the new list() implementation can not only appear in lvalues, but can also be used in foreach loops:

<?php foreach ($points as ["x" => $x, "y" => $y]) {    
var_dump($x, $y);
Copy after login

However, due to implementation issues, list() and [] cannot be used nested in each other:

<?php // 不合法
list([$a, $b], [$c, $d]) = [[1, 2], [3, 4]]; 
// 不合法
[list($a, $b), list($c, $d)] = [[1, 2], [3, 4]]; 
// 合法
[[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];
Copy after login

Allows the key to be specified in the list

As mentioned above, new The key can be specified in the implementation of list():

<?php $array = ["a" => 1, "b" => 2, "c" => 3];["a" => $a, "b" => $b, "c" => $c] = $array;
Copy after login

This is equivalent to:

<?php $a = $array[&#39;a&#39;];$b = $array[&#39;b&#39;];$c = $array[&#39;c&#39;];
Copy after login

The difference from the past is that the previous implementation of list() is equivalent to the key being only 0 , 1, 2, 3 and the order cannot be adjusted. Execute the following statement:

<?php list($a, $b) = [1 => &#39;1&#39;, 2 => &#39;2&#39;];
Copy after login

will get the error PHP error: Undefined offset: 0... .

The new implementation can adjust the assignment in the following ways:

<?php list(1 => $a, 2 => $b) = [1 => &#39;1&#39;, 2 => &#39;2&#39;];
Copy after login

Unlike arrays, list does not support mixed keys. The following writing will trigger a parsing error. :

<?php // Parse error: syntax error, ...list($unkeyed, "key" => $keyed) = $array;
Copy after login

For more complex situations, list also supports composite form parsing:

<?php $points = [    
["x" => 1, "y" => 2],    
["x" => 2, "y" => 1]]; 
list(list("x" => $x1, "y" => $y1), list("x" => $x2, "y" => $y2)) = $points; 
$points = [    
"first" => [1, 2],    
"second" => [2, 1]]; 
list("first" => list($x1, $y1), 
"second" => list($x2, $y2)) = $points;
Copy after login

and used 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;}
Copy after login

void return type

PHP7 .0 adds the feature of specifying function return type, but the return type cannot be specified as void. This feature of 7.1 is a supplement:

<?php function should_return_nothing(): void {    return 1; // Fatal error: A void function must not return a value}
Copy after login

The following two situations can be verified:

<?php function lacks_return(): void {    // valid} function returns_nothing(): void {    return; // valid}
Copy after login

A function defined with 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}
Copy after login

In addition, void is only applicable to the return type and cannot be used for parameter type declaration, or it will trigger an error:

<?php function foobar(void $foo) { // Fatal error: void cannot be used as a parameter type}
Copy after login

The declaration of return type in a class function cannot be overridden by a subclass, otherwise an error will be triggered:

<?php class Foo{    
public function bar(): void {    
}
} 
class Foobar extends Foo{   
 public function bar(): array { // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void    }}
Copy after login

Class constant attribute setting

This feature It is relatively simple to say, that is, the constants in the class now support the use of public, private and protected modifications:

<?php class Token {    
// 常量默认为 
public    const PUBLIC_CONST = 0;     
// 可以自定义常量的可见范围    
private const PRIVATE_CONST = 0;    
protected const PROTECTED_CONST = 0;    
public const PUBLIC_CONST_TWO = 0;     
// 多个常量同时声明只能有一个属性    
private const FOO = 1, BAR = 2;}
Copy after login

In addition, the constants in the interface (interface) can only be public attributes:

<?php interface ICache {    public const PUBLIC = 0;    const IMPLICIT_PUBLIC = 1;}
Copy after login

In order to cope with the changes, the implementation of the reflection class has also been enriched accordingly, and two methods getReflectionConstant and getReflectionConstants have been added to obtain additional attributes of constants:

<?php class testClass  {    
const TEST_CONST = &#39;test&#39;;
} 
$obj = new ReflectionClass( "testClass" );
$const = $obj->getReflectionConstant( "TEST_CONST" );
$consts = $obj->getReflectionConstants();
Copy after login

Multi-condition catch

In the past In the try...catch statement, each catch can only set one conditional judgment:

<?php try {    
// Some code...
} 
catch (ExceptionType1 $e) {    
// 处理 ExceptionType1} 
catch (ExceptionType2 $e) {    
// 处理 ExceptionType2} 
catch (Exception $e) {    // ...}
Copy after login

In the new implementation, multiple conditions can be set in one catch, which is equivalent to the form judgment of or:

<?php try {    
// Some code...} 
catch (ExceptionType1 | ExceptionType2 $e) {    
// 对于 ExceptionType1 和 ExceptionType2 的处理} 
catch (Exception $e) {    // ...}
Copy after login

The above is the list of new features of PHP 7.1. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template