Home  >  Article  >  Backend Development  >  PHP 7 new features

PHP 7 new features

高洛峰
高洛峰Original
2017-02-09 10:22:322301browse

The best language has released a new version, an epoch-making version: PHP7.

PHP7 has fixed a lot of bugs and added new functions and syntactic sugar. These changes involve familiar and unfamiliar core functions and expansion packages such as core packages, GD libraries, PDO, ZIP, and ZLIB.

PHP7 has removed functions that have been abandoned. For example, the mysql_ series of functions were abandoned in PHP5.5 and deleted in PHP7.

The performance of PHP7 is higher than HHVM. And twice as fast as PHP5.6.

http://www.php.cn/年12月3号PHP开发团队宣布PHP 7.0.0即将上市。本次发布标志着新的重要的PHP 7系列的开始。 PHP 7.0.0附带了一个新版本的Zend引擎中,无数的改进和新功能,如性能改善:PHP 7高达两倍快的PHP 5.6显著减少内存使用抽象语法树一致的64位支持改进的异常层次结构许多转化为异常致命错误安全随机数发生器删除旧的和不支持的SAPIs和扩展空合并运算符(?)返回和标量类型声明匿名类零成本断言 这是下一个主要版本的PHP。它的发布是近两年的发展征程的结果。这是核心团队的一个非常特殊的成就。而且,它是许多活跃的社区成员难以置信努力的结果。事实上,这是一个新的PHP一代的崛起与巨大潜力。恭喜大家,这是一个壮观的PHP的世界!感谢感谢所有的贡献者和支持者!

According to the change log, I sorted out the class libraries involved: Core, CLI_server, COM, Curl, Date, DBA, DOM, EXIF, Fileinfo, Filter, FPM, FTP , GD, GMP, hash, IMAP, Intl, JSON, LDAP, LiteSpeed, libxml, Mcrypt, Mysqli, OCI8, ODBC, Opcache, OpenSSL, Pcntl, PCRE, PDO, PDO_DBlib, PDO_mysql, PDO_OCI, PDO_pgsql, Phar, Phpdbg, Reflection , Session, OAP, SPL, SQLite3, tandard, Streams, Tokenizer, XMLReader, XMLRPC, XSL, Zlib, Zip

1. The past and present life of PHP7

The following is excerpted and modified from WeChat with Brother Niao

PHP7 started during the Spring Festival of 2014, because the Opcache JIT based on PHP-5.5 was shelved because it could not meet expectations, and it made Brother Niao and others realize that the basic part was not good enough and could not be well supported. JIT, so I started a refactoring project, hoping to get an improvement of more than 30%. Later, we found that the performance improvement was greater than we imagined, so we named it the PHP NG project.

After initiating the voting, most people supported the PHP NG project and decided to develop a new version of PHP based on PHP NG. The community had developed PHP6. Later, the features of PHP6 were gradually implemented in PHP5.5, 5.6 and other versions, so PHP6 was shelved. After a community vote, the new project was named PHP7.

In the past two years, various new features have been added, performance has been continuously improved, many previously unreasonable improvements have been added to PHP7, making PHP7 more and more full-featured. From the changes in the lowest level ZVAL to scalar type hints, from the initial 30% performance improvement to the current performance leap of more than 100%, every change is worth looking forward to. Then after several reluctant delays, Finally, today, all this will be presented to you.

2. Installation

Installation: We compiled the core package as well as PDO, GD, mysqli, Zip, etc.

>$ ./configure --prefix=/usr/local/php7 --enable-fpm --with-zlib --enable-mbstring --with-openssl --with-mysqli --with-mysql-sock --with-gd --enable-gd-native-ttf  --enable-pdo --with-pdo-mysql --with-gettext --with-curl --with-pdo-mysql --enable-sockets --enable-bcmath --enable-xml --with-bz2 --enable-zip -enable-pcntl>$ make>$ sudo make install

PHP 7 新特征

3. Test

Test version:

Old version PHP 5.5.29, new version PHP 7.0.0

PHP 7 新特征PHP 7 new features

The test machine configuration is as follows:

PHP 7 新特征

1. Test case one:

Generate half a million arrays , and query whether the key exists 500,000 times

The test results are as follows:

➜ time php test.php

php test.php

0.60 s user

0.05s system

98% cpu

0.667 total

➜ time /usr/local/php7/bin/php test.php

/usr/local/php7/bin/php test.php

0.05s user

0.02s system

92% cpu

0.073 total

PHP7 is 9 times faster than PHP5.5

2. Test case 2:

Generate half a million arrays, and Check whether the value exists 500,000 times

➜ time php test.php

php test.php

0.79s user

0.01s system

99% cpu

0.809 total

➜ time /usr/local/php7/bin/php test.php

/usr/local/php7/ bin/php test.php

0.08s user

0.01s system

97% cpu

0.091 total

PHP7 is 8.7 times faster than PHP5.5

3. Test case three:

Examples and results are taken from Niao Ge’s blog. Based on WordPress, test PHP7 and HHVM3.2. Use Apache's ab testing tool. 100 concurrency, 10,000 requests. 100 requests will be used to warm up before testing.

PHP7 results are as follows:

Concurrency Level: 100

Time taken for tests: 38.726 seconds

Complete requests: 10000

Failed requests:       0

Write errors:       0

Total transferred:     89290000 bytes

HTML transferred:       86900000 bytes

Requests per second:   258.22 [#/sec ] (mean)

Time per request: 387.260 [ms] (mean)

Time per request: 3.873 [ms] (mean, across all concurrent requests)

Transfer rate:       2251.64 [Kbytes/sec] received

HHVM-3.2

 

HHVM结果如下:

Document Path:          /wordpress/

Document Length:        8690 bytes

Concurrency Level:      100

Time taken for tests:   43.296 seconds

Complete requests:      10000

Failed requests:        0

Write errors:           0

Total transferred:      89260000 bytes

HTML transferred:       86900000 bytes

Requests per second:    230.97 [#/sec] (mean)

Time per request:       432.957 [ms] (mean)

Time per request:       4.330 [ms] (mean, across all concurrent requests)

Transfer rate:          2013.31 [Kbytes/sec] received

 

PHP7 – 258.22 QPS HHVM – 230.97 QPS

四、新特性

1、标量类型声明

有两种模式: 强制 (默认) 和 严格模式。 现在可以使用下列类型参数(无论用强制模式还是严格模式): 字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool)。它们扩充了PHP5中引入的其他类型:类名,接口,数组和 回调类型。在旧版中,函数的参数申明只能是(Array $arr)、(CLassName $obj)等,基本类型比如Int,String等是不能够被申明的

若无强制类型转换,会输入int(1)bool(true)。转换后会输出bool(true) bool(true)

2、返回值类型声明

PHP 7 增加了对返回类型声明的支持。返回类型声明指明了函数返回值的类型。可用的类型与参数声明中可用的类型相同。

以上例程会输出:

Array

(

[0] => 6

    [1] => 15

    [2] => 24

)

3、null合并运算符

项目中存在大量同时使用三元表达式和 isset()的情况,新增了null合并运算符 (??) 这个语法糖。如果变量存在且值不为NULL, 它就会返回自身的值,否则返回它的第二个操作数。

旧版:isset($_GET[‘id']) ? $_GET[id] : err;

新版:$_GET['id'] ?? 'err';

4、太空船操作符(组合比较符)

            太空船操作符用于比较两个表达式。当$a小于、等于或大于$b时它分别返回-1、0或1。 比较的原则是沿用 PHP 的常规比较规则进行的。

 1; // 0echo 1 <=> 2; // -1echo 2 <=> 1; // 1// Floatsecho 1.5 <=> 1.5; // 0echo 1.5 <=> 2.5; // -1echo 2.5 <=> 1.5; // 1// Stringsecho "a" <=> "a"; // 0echo "a" <=> "b"; // -1echo "b" <=> "a"; // 1?>

5、通过define()定义常量数组

6、匿名类

现在支持通过new class 来实例化一个匿名类,这可以用来替代一些“用后即焚”的完整类定义。

logger;    }    public function setLogger(Logger $logger) {        $this->logger = $logger;    }}$app = new Application;$app->setLogger(new class implements Logger {    public function log(string $msg) {        echo $msg;    }});var_dump($app->getLogger());

7、Unicode codepoint 转译语法

这接受一个以16进制形式的 Unicode codepoint,并打印出一个双引号或heredoc包围的 UTF-8 编码格式的字符串。 可以接受任何有效的 codepoint,并且开头的 0 是可以省略的。

            旧版输出:u{9876}

            新版输入:顶

8、Closure::call()

Closure::call() 现在有着更好的性能,简短干练的暂时绑定一个方法到对象上闭包并调用它。

name;};$name = $getNameFunc->bindTo(new Test, 'Test');echo $name();//PHP7可以,PHP5.6报错$getX = function() {return $this->name;};echo $getX->call(new Test);

9、为unserialize()提供过滤

这个特性旨在提供更安全的方式解包不可靠的数据。它通过白名单的方式来防止潜在的代码注入。

 false]);//将所有对象分为__PHP_Incomplete_Class 对象 除了ClassName1和ClassName2$data = unserialize($foo, ["allowed_classes" => ["ClassName1", "ClassName2"]);//默认行为,和 unserialize($foo)相同$data = unserialize($foo, ["allowed_classes" => true]);

10、IntlChar

            新增加的 IntlChar 类旨在暴露出更多的 ICU 功能。这个类自身定义了许多静态方法用于操作多字符集的 unicode 字符。Intl是Pecl扩展,使用前需要编译进PHP中,也可apt-get/yum/port install php5-intl

以上例程会输出:

10ffff

COMMERCIAL AT

bool(true)

11、预期

预期是向后兼用并增强之前的 assert() 的方法。 它使得在生产环境中启用断言为零成本,并且提供当断言失败时抛出特定异常的能力。 老版本的API出于兼容目的将继续被维护,assert()现在是一个语言结构,它允许第一个参数是一个表达式,而不仅仅是一个待计算的 string或一个待测试的boolean。

以上例程会输出:

Fatal error: Uncaught CustomError: Some error message

12、Group use declarations

            从同一 namespace 导入的类、函数和常量现在可以通过单个 use 语句 一次性导入了。

13、intp()

            接收两个参数作为被除数和除数,返回他们相除结果的整数部分。

输出int(3)

14、CSPRNG

新增两个函数: random_bytes() and random_int().可以加密的生产被保护的整数和字符串。我这蹩脚的翻译,总之随机数变得安全了。

random_bytes — 加密生存被保护的伪随机字符串

random_int —加密生存被保护的伪随机整数

15、preg_replace_callback_array()

            新增了一个函数preg_replace_callback_array(),使用该函数可以使得在使用preg_replace_callback()函数时代码变得更加优雅。在PHP7之前,回调函数会调用每一个正则表达式,回调函数在部分分支上是被污染了。

16、Session options

            现在,session_start()函数可以接收一个数组作为参数,可以覆盖php.ini中session的配置项。

            比如,把cache_limiter设置为私有的,同时在阅读完session后立即关闭。

 'private',    'read_and_close' => true,]);?>

17、生成器的返回值

            在PHP5.5引入生成器的概念。生成器函数每执行一次就得到一个yield标识的值。在PHP7中,当生成器迭代完成后,可以获取该生成器函数的返回值。通过Generator::getReturn()得到。

getReturn();?>

输出为:1 2 3 a

18、生成器中引入其他生成器

            在生成器中可以引入另一个或几个生成器,只需要写yield from functionName1

输出:1 2 3 4 5 6

五、不兼容性

1、foreach不再改变内部数组指针

在PHP7之前,当数组通过 foreach 迭代时,数组指针会移动。现在开始,不再如此,见下面代码。

PHP5输出:

int(1)

int(2)

bool(false)

PHP7输出:

int(0)

int(0)

int(0)

2、foreach通过引用遍历时,有更好的迭代特性

当使用引用遍历数组时,现在 foreach 在迭代中能更好的跟踪变化。例如,在迭代中添加一个迭代值到数组中,参考下面的代码:

PHP5输出:

int(0)

PHP7输出:

int(0)

int(1)

3、十六进制字符串不再被认为是数字

    含十六进制字符串不再被认为是数字

PHP5输出:

bool(true)

bool(true)

int(15)

string(2) "oo"

PHP7输出:

bool(false)

bool(false)

int(0)

Notice: A non well formed numeric value encountered in /tmp/test.php on line 5

string(3) "foo"

4、PHP7中被移除的函数

被移除的函数列表如下:

call_user_func() 和 call_user_func_array()从PHP 4.1.0开始被废弃。

已废弃的 mcrypt_generic_end() 函数已被移除,请使用mcrypt_generic_deinit()代替。

已废弃的 mcrypt_ecb(), mcrypt_cbc(), mcrypt_cfb() 和 mcrypt_ofb() 函数已被移除。

set_magic_quotes_runtime(), 和它的别名 magic_quotes_runtime()已被移除. 它们在PHP 5.3.0中已经被废弃,并且 在in PHP 5.4.0也由于魔术引号的废弃而失去功能。

已废弃的 set_socket_blocking() 函数已被移除,请使用stream_set_blocking()代替。

dl()在 PHP-FPM 不再可用,在 CLI 和 embed SAPIs 中仍可用。

GD库中下列函数被移除:imagepsbbox()、imagepsencodefont()、imagepsextendfont()、imagepsfreefont()、imagepsloadfont()、imagepsslantfont()、imagepstext()

在配置文件php.ini中,always_populate_raw_post_data、asp_tags、xsl.security_prefs被移除了。

5、new 操作符创建的对象不能以引用方式赋值给变量

new 操作符创建的对象不能以引用方式赋值给变量

PHP5输出:

Deprecated: Assigning the return value of new by reference is deprecated in /tmp/test.php on line 3

PHP7输出:

Parse error: syntax error, unexpected 'new' (T_NEW) in /tmp/test.php on line 3

6、移除了 ASP 和 script PHP 标签

使用类似 ASP 的标签,以及 script 标签来区分 PHP 代码的方式被移除。 受到影响的标签有:<% %>、<%= %>、

7、从不匹配的上下文发起调用

            在不匹配的上下文中以静态方式调用非静态方法, 在 PHP 5.6 中已经废弃, 但是在 PHP 7.0 中, 会导致被调用方法中未定义 $this 变量,以及此行为已经废弃的警告。

callNonStaticMethodOfA();?>

PHP5输出:

Deprecated: Non-static method A::test() should not be called statically, assuming $this from incompatible context in /tmp/test.php on line 8

object(B)#1 (0) {

}

PHP7输出:

Deprecated: Non-static method A::test() should not be called statically in /tmp/test.php on line 8

Notice: Undefined variable: this in /tmp/test.php on line 3

NULL

8、在数值溢出的时候,内部函数将会失败

将浮点数转换为整数的时候,如果浮点数值太大,导致无法以整数表达的情况下, 在之前的版本中,内部函数会直接将整数截断,并不会引发错误。 在 PHP 7.0 中,如果发生这种情况,会引发 E_WARNING 错误,并且返回 NULL。

9、JSON 扩展已经被 JSOND 取代

JSON 扩展已经被 JSOND 扩展取代。 对于数值的处理,有以下两点需要注意的: 第一,数值不能以点号(.)结束 (例如,数值 34. 必须写作 34.0 或 34)。 第二,如果使用科学计数法表示数值,e 前面必须不是点号(.) (例如,3.e3 必须写作 3.0e3 或 3e3)。

10、INI 文件中 # 注释格式被移除

在配置文件INI文件中,不再支持以 # 开始的注释行, 请使用 ;(分号)来表示注释。 此变更适用于 php.ini 以及用 parse_ini_file() 和 parse_ini_string() 函数来处理的文件。

11、$HTTP_RAW_POST_DATA 被移除

不再提供 $HTTP_RAW_POST_DATA 变量。 请使用 php://input 作为替代。

12、yield 变更为右联接运算符

在使用 yield 关键字的时候,不再需要括号, 并且它变更为右联接操作符,其运算符优先级介于 print 和 => 之间。 这可能导致现有代码的行为发生改变。可以通过使用括号来消除歧义。

 

PHP官方网站文档:http://www.php.cn/

可以浏览PHP5.6到PHP7时,新特性、新增函数、已经被移除的函数、不兼容性、新增的类和接口等内容。

引自:PHP7发布。最好语言发布了新版本


The best language has released a new version, an epoch-making version: PHP7.

PHP7 has fixed a lot of bugs and added new functions and syntactic sugar. These changes involve familiar and unfamiliar core functions and expansion packages such as core packages, GD libraries, PDO, ZIP, and ZLIB.

PHP7 has removed functions that have been abandoned. For example, the mysql_ series of functions were abandoned in PHP5.5 and deleted in PHP7.

The performance of PHP7 is higher than HHVM. And twice as fast as PHP5.6.

http://www.php.cn/年12月3号PHP开发团队宣布PHP 7.0.0即将上市。本次发布标志着新的重要的PHP 7系列的开始。 PHP 7.0.0附带了一个新版本的Zend引擎中,无数的改进和新功能,如性能改善:PHP 7高达两倍快的PHP 5.6显著减少内存使用抽象语法树一致的64位支持改进的异常层次结构许多转化为异常致命错误安全随机数发生器删除旧的和不支持的SAPIs和扩展空合并运算符(?)返回和标量类型声明匿名类零成本断言 这是下一个主要版本的PHP。它的发布是近两年的发展征程的结果。这是核心团队的一个非常特殊的成就。而且,它是许多活跃的社区成员难以置信努力的结果。事实上,这是一个新的PHP一代的崛起与巨大潜力。恭喜大家,这是一个壮观的PHP的世界!感谢感谢所有的贡献者和支持者!

According to the change log, I sorted out the class libraries involved: Core, CLI_server, COM, Curl, Date, DBA, DOM, EXIF, Fileinfo, Filter, FPM, FTP , GD, GMP, hash, IMAP, Intl, JSON, LDAP, LiteSpeed, libxml, Mcrypt, Mysqli, OCI8, ODBC, Opcache, OpenSSL, Pcntl, PCRE, PDO, PDO_DBlib, PDO_mysql, PDO_OCI, PDO_pgsql, Phar, Phpdbg, Reflection , Session, OAP, SPL, SQLite3, tandard, Streams, Tokenizer, XMLReader, XMLRPC, XSL, Zlib, Zip

1. The past and present life of PHP7

The following is excerpted and modified from WeChat with Brother Niao

PHP7 started during the Spring Festival of 2014, because the Opcache JIT based on PHP-5.5 was shelved because it could not meet expectations, and it made Brother Niao and others realize that the basic part was not good enough and could not be well supported. JIT, so I started a refactoring project, hoping to get an improvement of more than 30%. Later, we found that the performance improvement was greater than we imagined, so we named it the PHP NG project.

After initiating the voting, most people supported the PHP NG project and decided to develop a new version of PHP based on PHP NG. The community had developed PHP6. Later, the features of PHP6 were gradually implemented in PHP5.5, 5.6 and other versions, so PHP6 was shelved. After a community vote, the new project was named PHP7.

In the past two years, various new features have been added, performance has been continuously improved, many previously unreasonable improvements have been added to PHP7, making PHP7 more and more full-featured. From the changes in the lowest level ZVAL to scalar type hints, from the initial 30% performance improvement to the current performance leap of more than 100%, every change is worth looking forward to. Then after several reluctant delays, Finally, today, all this will be presented to you.

2. Installation

Installation: We compiled the core package as well as PDO, GD, mysqli, Zip, etc.

>$ ./configure --prefix=/usr/local/php7 --enable-fpm --with-zlib --enable-mbstring --with-openssl --with-mysqli --with-mysql-sock --with-gd --enable-gd-native-ttf  --enable-pdo --with-pdo-mysql --with-gettext --with-curl --with-pdo-mysql --enable-sockets --enable-bcmath --enable-xml --with-bz2 --enable-zip -enable-pcntl>$ make>$ sudo make install

PHP 7 新特征

3. Test

Test version:

Old version PHP 5.5.29, new version PHP 7.0.0

PHP 7 新特征PHP 7 new features

The test machine configuration is as follows:

PHP 7 新特征

1. Test case one:

Generate half a million arrays , and query whether the key exists 500,000 times

The test results are as follows:

➜ time php test.php

php test.php

0.60 s user

0.05s system

98% cpu

0.667 total

➜ time /usr/local/php7/bin/php test.php

/usr/local/php7/bin/php test.php

0.05s user

0.02s system

92% cpu

0.073 total

PHP7 is 9 times faster than PHP5.5

2. Test case 2:

Generate half a million arrays, and Check whether the value exists 500,000 times

➜ time php test.php

php test.php

0.79s user

0.01s system

99% cpu

0.809 total

➜ time /usr/local/php7/bin/php test.php

/usr/local/php7/ bin/php test.php

0.08s user

0.01s system

97% cpu

0.091 total

PHP7 is 8.7 times faster than PHP5.5

3. Test case three:

Examples and results are taken from Niao Ge’s blog. Based on WordPress, test PHP7 and HHVM3.2. Use Apache's ab testing tool. 100 concurrency, 10,000 requests. 100 requests will be used to warm up before testing.

PHP7 results are as follows:

Concurrency Level: 100

Time taken for tests: 38.726 seconds

Complete requests: 10000

Failed requests:       0

Write errors:       0

Total transferred:     89290000 bytes

HTML transferred:       86900000 bytes

Requests per second:   258.22 [#/sec ] (mean)

Time per request: 387.260 [ms] (mean)

Time per request: 3.873 [ms] (mean, across all concurrent requests)

Transfer rate:       2251.64 [Kbytes/sec] received

HHVM-3.2

 

HHVM结果如下:

Document Path:          /wordpress/

Document Length:        8690 bytes

Concurrency Level:      100

Time taken for tests:   43.296 seconds

Complete requests:      10000

Failed requests:        0

Write errors:           0

Total transferred:      89260000 bytes

HTML transferred:       86900000 bytes

Requests per second:    230.97 [#/sec] (mean)

Time per request:       432.957 [ms] (mean)

Time per request:       4.330 [ms] (mean, across all concurrent requests)

Transfer rate:          2013.31 [Kbytes/sec] received

 

PHP7 – 258.22 QPS HHVM – 230.97 QPS

四、新特性

1、标量类型声明

有两种模式: 强制 (默认) 和 严格模式。 现在可以使用下列类型参数(无论用强制模式还是严格模式): 字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool)。它们扩充了PHP5中引入的其他类型:类名,接口,数组和 回调类型。在旧版中,函数的参数申明只能是(Array $arr)、(CLassName $obj)等,基本类型比如Int,String等是不能够被申明的

若无强制类型转换,会输入int(1)bool(true)。转换后会输出bool(true) bool(true)

2、返回值类型声明

PHP 7 增加了对返回类型声明的支持。返回类型声明指明了函数返回值的类型。可用的类型与参数声明中可用的类型相同。

以上例程会输出:

Array

(

[0] => 6

    [1] => 15

    [2] => 24

)

3、null合并运算符

项目中存在大量同时使用三元表达式和 isset()的情况,新增了null合并运算符 (??) 这个语法糖。如果变量存在且值不为NULL, 它就会返回自身的值,否则返回它的第二个操作数。

旧版:isset($_GET[‘id']) ? $_GET[id] : err;

新版:$_GET['id'] ?? 'err';

4、太空船操作符(组合比较符)

            太空船操作符用于比较两个表达式。当$a小于、等于或大于$b时它分别返回-1、0或1。 比较的原则是沿用 PHP 的常规比较规则进行的。

 1; // 0echo 1 <=> 2; // -1echo 2 <=> 1; // 1// Floatsecho 1.5 <=> 1.5; // 0echo 1.5 <=> 2.5; // -1echo 2.5 <=> 1.5; // 1// Stringsecho "a" <=> "a"; // 0echo "a" <=> "b"; // -1echo "b" <=> "a"; // 1?>

5、通过define()定义常量数组

6、匿名类

现在支持通过new class 来实例化一个匿名类,这可以用来替代一些“用后即焚”的完整类定义。

logger;    }    public function setLogger(Logger $logger) {        $this->logger = $logger;    }}$app = new Application;$app->setLogger(new class implements Logger {    public function log(string $msg) {        echo $msg;    }});var_dump($app->getLogger());

7、Unicode codepoint 转译语法

这接受一个以16进制形式的 Unicode codepoint,并打印出一个双引号或heredoc包围的 UTF-8 编码格式的字符串。 可以接受任何有效的 codepoint,并且开头的 0 是可以省略的。

            旧版输出:u{9876}

            新版输入:顶

8、Closure::call()

Closure::call() 现在有着更好的性能,简短干练的暂时绑定一个方法到对象上闭包并调用它。

name;};$name = $getNameFunc->bindTo(new Test, 'Test');echo $name();//PHP7可以,PHP5.6报错$getX = function() {return $this->name;};echo $getX->call(new Test);

9、为unserialize()提供过滤

这个特性旨在提供更安全的方式解包不可靠的数据。它通过白名单的方式来防止潜在的代码注入。

 false]);//将所有对象分为__PHP_Incomplete_Class 对象 除了ClassName1和ClassName2$data = unserialize($foo, ["allowed_classes" => ["ClassName1", "ClassName2"]);//默认行为,和 unserialize($foo)相同$data = unserialize($foo, ["allowed_classes" => true]);

10、IntlChar

            新增加的 IntlChar 类旨在暴露出更多的 ICU 功能。这个类自身定义了许多静态方法用于操作多字符集的 unicode 字符。Intl是Pecl扩展,使用前需要编译进PHP中,也可apt-get/yum/port install php5-intl

以上例程会输出:

10ffff

COMMERCIAL AT

bool(true)

11、预期

预期是向后兼用并增强之前的 assert() 的方法。 它使得在生产环境中启用断言为零成本,并且提供当断言失败时抛出特定异常的能力。 老版本的API出于兼容目的将继续被维护,assert()现在是一个语言结构,它允许第一个参数是一个表达式,而不仅仅是一个待计算的 string或一个待测试的boolean。

以上例程会输出:

Fatal error: Uncaught CustomError: Some error message

12、Group use declarations

            从同一 namespace 导入的类、函数和常量现在可以通过单个 use 语句 一次性导入了。

13、intp()

            接收两个参数作为被除数和除数,返回他们相除结果的整数部分。

输出int(3)

14、CSPRNG

新增两个函数: random_bytes() and random_int().可以加密的生产被保护的整数和字符串。我这蹩脚的翻译,总之随机数变得安全了。

random_bytes — 加密生存被保护的伪随机字符串

random_int —加密生存被保护的伪随机整数

15、preg_replace_callback_array()

            新增了一个函数preg_replace_callback_array(),使用该函数可以使得在使用preg_replace_callback()函数时代码变得更加优雅。在PHP7之前,回调函数会调用每一个正则表达式,回调函数在部分分支上是被污染了。

16、Session options

            现在,session_start()函数可以接收一个数组作为参数,可以覆盖php.ini中session的配置项。

            比如,把cache_limiter设置为私有的,同时在阅读完session后立即关闭。

 'private',    'read_and_close' => true,]);?>

17、生成器的返回值

            在PHP5.5引入生成器的概念。生成器函数每执行一次就得到一个yield标识的值。在PHP7中,当生成器迭代完成后,可以获取该生成器函数的返回值。通过Generator::getReturn()得到。

getReturn();?>

输出为:1 2 3 a

18、生成器中引入其他生成器

            在生成器中可以引入另一个或几个生成器,只需要写yield from functionName1

输出:1 2 3 4 5 6

五、不兼容性

1、foreach不再改变内部数组指针

在PHP7之前,当数组通过 foreach 迭代时,数组指针会移动。现在开始,不再如此,见下面代码。

PHP5输出:

int(1)

int(2)

bool(false)

PHP7输出:

int(0)

int(0)

int(0)

2、foreach通过引用遍历时,有更好的迭代特性

当使用引用遍历数组时,现在 foreach 在迭代中能更好的跟踪变化。例如,在迭代中添加一个迭代值到数组中,参考下面的代码:

PHP5输出:

int(0)

PHP7输出:

int(0)

int(1)

3、十六进制字符串不再被认为是数字

    含十六进制字符串不再被认为是数字

PHP5输出:

bool(true)

bool(true)

int(15)

string(2) "oo"

PHP7输出:

bool(false)

bool(false)

int(0)

Notice: A non well formed numeric value encountered in /tmp/test.php on line 5

string(3) "foo"

4、PHP7中被移除的函数

被移除的函数列表如下:

call_user_func() 和 call_user_func_array()从PHP 4.1.0开始被废弃。

已废弃的 mcrypt_generic_end() 函数已被移除,请使用mcrypt_generic_deinit()代替。

已废弃的 mcrypt_ecb(), mcrypt_cbc(), mcrypt_cfb() 和 mcrypt_ofb() 函数已被移除。

set_magic_quotes_runtime(), 和它的别名 magic_quotes_runtime()已被移除. 它们在PHP 5.3.0中已经被废弃,并且 在in PHP 5.4.0也由于魔术引号的废弃而失去功能。

已废弃的 set_socket_blocking() 函数已被移除,请使用stream_set_blocking()代替。

dl()在 PHP-FPM 不再可用,在 CLI 和 embed SAPIs 中仍可用。

GD库中下列函数被移除:imagepsbbox()、imagepsencodefont()、imagepsextendfont()、imagepsfreefont()、imagepsloadfont()、imagepsslantfont()、imagepstext()

在配置文件php.ini中,always_populate_raw_post_data、asp_tags、xsl.security_prefs被移除了。

5、new 操作符创建的对象不能以引用方式赋值给变量

new 操作符创建的对象不能以引用方式赋值给变量

PHP5输出:

Deprecated: Assigning the return value of new by reference is deprecated in /tmp/test.php on line 3

PHP7输出:

Parse error: syntax error, unexpected 'new' (T_NEW) in /tmp/test.php on line 3

6、移除了 ASP 和 script PHP 标签

使用类似 ASP 的标签,以及 script 标签来区分 PHP 代码的方式被移除。 受到影响的标签有:<% %>、<%= %>、

7、从不匹配的上下文发起调用

            在不匹配的上下文中以静态方式调用非静态方法, 在 PHP 5.6 中已经废弃, 但是在 PHP 7.0 中, 会导致被调用方法中未定义 $this 变量,以及此行为已经废弃的警告。

callNonStaticMethodOfA();?>

PHP5输出:

Deprecated: Non-static method A::test() should not be called statically, assuming $this from incompatible context in /tmp/test.php on line 8

object(B)#1 (0) {

}

PHP7输出:

Deprecated: Non-static method A::test() should not be called statically in /tmp/test.php on line 8

Notice: Undefined variable: this in /tmp/test.php on line 3

NULL

8、在数值溢出的时候,内部函数将会失败

将浮点数转换为整数的时候,如果浮点数值太大,导致无法以整数表达的情况下, 在之前的版本中,内部函数会直接将整数截断,并不会引发错误。 在 PHP 7.0 中,如果发生这种情况,会引发 E_WARNING 错误,并且返回 NULL。

9、JSON 扩展已经被 JSOND 取代

JSON 扩展已经被 JSOND 扩展取代。 对于数值的处理,有以下两点需要注意的: 第一,数值不能以点号(.)结束 (例如,数值 34. 必须写作 34.0 或 34)。 第二,如果使用科学计数法表示数值,e 前面必须不是点号(.) (例如,3.e3 必须写作 3.0e3 或 3e3)。

10、INI 文件中 # 注释格式被移除

在配置文件INI文件中,不再支持以 # 开始的注释行, 请使用 ;(分号)来表示注释。 此变更适用于 php.ini 以及用 parse_ini_file() 和 parse_ini_string() 函数来处理的文件。

11、$HTTP_RAW_POST_DATA 被移除

不再提供 $HTTP_RAW_POST_DATA 变量。 请使用 php://input 作为替代。

12、yield 变更为右联接运算符

在使用 yield 关键字的时候,不再需要括号, 并且它变更为右联接操作符,其运算符优先级介于 print 和 => 之间。 这可能导致现有代码的行为发生改变。可以通过使用括号来消除歧义。

 更多PHP 7 新特征相关文章请关注PHP中文网!

Statement:
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
Previous article:PHP execution principleNext article:PHP execution principle