Home>Article>Backend Development> New features from PHP5.5 to PHP7.2

New features from PHP5.5 to PHP7.2

藏色散人
藏色散人 forward
2019-03-28 17:17:31 3338browse


1. Migrate from PHP 5.5.x to PHP 5.6.x

Use expressions to define constants

In previous versions of PHP, static values had to be used to define constants, declare properties, and specify default values for function parameters. You can now use numeric expressions including numbers, string literals, and other constants to define constants, declare properties, and set default values for function parameters.


      

Now you can define constants of typearraythrough theconstkeyword.


      

Use...operators to define variable-length parameter functions

Now you can not rely onfunc_get_args(), Use...operators to implement variable-length parameter functions.

The above routine will output:

$req: 1; $opt: 0; number of params: 0 $req: 1; $opt: 2; number of params: 0 $req: 1; $opt: 2; number of params: 1 $req: 1; $opt: 2; number of params: 2

Use...operators for parameter expansion

When calling the function When using...operators, expand arrays and traversable objects into function parameters. In other programming languages, such as Ruby, this is called the concatenation operator.

The above routine will output:

6

use function and use const

use operator has been extended to support Import external functions and constants into the class. The corresponding structures areuse functionanduse const.

The above routine will output:

42 Name\Space\f

Usehash_equals()Compare strings to avoid timing attacks

2. From PHP 5.6 .x Ported to PHP 7.0.x

Scalar type declaration

Scalar type declarations have two modes: mandatory (default) and strict mode. The following type parameters are now available (either in forced or strict mode): string, int, float, and bool.


      

The above routine will output:

int(9)

Return value type declaration

PHP 7 adds support for return type declaration. Similar to the parameter type declaration, the return type declaration specifies the type of the function's return value. The available types are the same as those available in the parameter declaration.


      

null coalescing operator

Due to the large number of situations where ternary expressions and isset() are used simultaneously in daily use, we added the null coalescing operator ( ??) This syntactic sugar. If the variable exists and is not NULL, it returns its own value, otherwise it returns its second operand.

Spaceship operator (combined comparison operator)

The spaceship operator is used to compare two expressions. It returns -1, 0 or 1 when $a is less than, equal to or greater than $b respectively. The principle of comparison follows PHP's regular comparison rules.

 '1'; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // 浮点数 echo '1.50' <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // 字符串 echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1 ?>

Define constant arrays viadefine()

Constants of type Array can now be defined viadefine(). In PHP5.6 it can only be defined viaconst.

define('ANIMALS', [ 'dog', 'cat', 'bird' ]); echo ANIMALS[1]; // 输出 "cat"

Closure::call()

Closure::call()Now has better performance, short and concise temporary binding Closure a method on the object and call it.

x;}; $getX = $getXCB->bindTo(new A, 'A'); // 中间层闭包 echo $getX(); // PHP 7+ 及更高版本的代码 $getX = function() {return $this->x;}; echo $getX->call(new A);

The above routine will output:

1

Group use declaration

Classes, functions and constants imported from the samenamespacecan now be passed through a single use The statement is imported at once.

Generators can return expressions

This feature builds on the generator feature introduced in PHP 5.5 version. It allows an expression to be returned by using the return syntax in a generator function (but does not allow the return of a reference value). The return value of the generator can be obtained by calling the Generator::getReturn() method, but this method can only be used when generating Called once after the processor has finished generating work.

Integer division functionintdiv()

##3. Migrating from PHP 7.0.x to PHP 7.1.x

Nullable types

The types of parameters and return values can now be nullable by adding a question mark before the type. When this feature is enabled, the parameters passed in or the result returned by the function are either of the given type or null .


      

The above routine will output:

string(10) "elePHPant" NULL string(10) "elePHPant" NULL Uncaught Error: Too few arguments to function test(), 0 passed in...

Void function

A new return value type void is introduced. Methods whose return values are declared of type void either simply omit the

returnstatement, or use an emptyreturnstatement. NULL is not a legal return value for void functions.


      

The above routine will output:

null int(2) int(1) Symmetric array destructuring

The short array syntax ([]) is now an alternative to the list() syntax and can be used to assign the value of the array to some variables ( included in foreach).


      

Class constant visibility

Now supports setting the visibility of class constants.


      

iterablePseudo class

现在引入了一个新的被称为iterable的伪类 (与callable类似)。 这可以被用在参数或者返回值类型中,它代表接受数组或者实现了Traversable接口的对象。 至于子类,当用作参数时,子类可以收紧父类的iterable类型到array 或一个实现了Traversable的对象。对于返回值,子类可以拓宽父类的 array或对象返回值类型到iterable。


      

多异常捕获处理

一个catch语句块现在可以通过管道字符(|)来实现多个异常的捕获。 这对于需要同时处理来自不同类的不同异常时很有用。


      

list()现在支持键名

现在list()和它的新的[]语法支持在它内部去指定键名。这意味着它可以将任意类型的数组 都赋值给一些变量(与短数组语法类似)

 1, "name" => 'Tom'], ["id" => 2, "name" => 'Fred'], ]; // list() style list("id" => $id1, "name" => $name1) = $data[0]; // [] style ["id" => $id1, "name" => $name1] = $data[0]; // list() style foreach ($data as list("id" => $id, "name" => $name)) { // logic here with $id and $name } // [] style foreach ($data as ["id" => $id, "name" => $name]) { // logic here with $id and $name }

四、从PHP 7.1.x 移植到 PHP 7.2.x

新的对象类型

这种新的对象类型, object, 引进了可用于逆变(contravariant)参数输入和协变(covariant)返回任何对象类型。


      

允许重写抽象方法(Abstract method)

当一个抽象类继承于另外一个抽象类的时候,继承后的抽象类可以重写被继承的抽象类的抽象方法。

abstract class A { abstract function test(string $s); } abstract class B extends A { // overridden - still maintaining contravariance for parameters and covariance for return abstract function test($s) : int; }

扩展了参数类型

重写方法和接口实现的参数类型现在可以省略了。不过这仍然是符合LSP,因为现在这种参数类型是逆变的。

interface A { public function Test(array $input); } class B implements A { public function Test($input){} // type omitted for $input }

允许分组命名空间的尾部逗号

命名空间可以在PHP 7中使用尾随逗号进行分组引入。

use Foo\Bar\{ Foo, Bar, Baz, };



The above is the detailed content of New features from PHP5.5 to PHP7.2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete