这一篇主要是来详细分析php7.0的新增功能。
一、性能与底层
PHP7速度是 PHP5.6 的两倍
php7 最显著的变化就是性能的极大提升,已接近Facebook开发的PHP执行引擎HHVM。在WordPress基准性能测试中,速度比5.6版本要快2~3倍,大大减少了内存占用。PHP7在语言上也有一些变化,比如添加返回类型声明、增加了一些新的保留关键字等。在安全方面,去除了PHP安全模式,添加魔术引号等。不仅如此,新版还支持64位,而且包含最新版Zend引擎。
测试一下
很简单的一个例子,生成一个 60 万元素的数组,通过查找key 的方式,来确定key是否存在。
$a = [];
for($i=0;$i<600000;$i++){
$a[$i] = $i;
}
foreach($a as $item) {
array_key_exists($item, $a);
}
我们分别在php5.6.11和php7.0.4来测试下性能。
php5.6.11
➜ time php 1.php
0.67s user 0.06s system 67% cpu 1.078 total
➜ time php 1.php
0.68s user 0.06s system 98% cpu 0.748 total
➜ time php 1.php
0.65s user 0.06s system 67% cpu 1.052 total
三次平均下来,大致是 user使用 0.65秒,system使用0.06秒,67%的cpu率。总共1秒左右。
再看php7的情况
➜ time /usr/local/opt/php70/bin/php 1.php
0.52s user 0.02s system 98% cpu 0.544 total
➜ time /usr/local/opt/php70/bin/php 1.php
0.49s user 0.02s system 99% cpu 0.513 total
➜ time /usr/local/opt/php70/bin/php 1.php
0.51s user 0.02s system 98% cpu 0.534 total
对比下来,user使用时间下降20%左右,system使用时间下降70%,cpu使用率更高高达98%。总体时间减少为。0.5秒。
这个例子看下来,效率提供了2倍。确实不错。
再看一个例子。同样也是生成一个 60 万元素的数组,查找 value是否存在。
$a = [];
for($i=0;$i<600000;$i++){
$a[$i] = $i;
}
foreach($a as $i) {
array_search($i, $a);
}
?>
先看php5.6.11
➜ testPHP time php 2.php
0.68s user 0.03s system 66% cpu 1.077 total
➜ testPHP time php 2.php
0.68s user 0.02s system 98% cpu 0.710 total
➜ testPHP time php 2.php
0.68s user 0.02s system 98% cpu 0.713 total
➜ testPHP time php 2.php
0.69s user 0.02s system 98% cpu 0.721 total
再接着看php7.0.4
➜ testPHP time /usr/local/opt/php70/bin/php 2.php
0.12s user 0.02s system 69% cpu 0.201 total
➜ testPHP time /usr/local/opt/php70/bin/php 2.php
0.11s user 0.01s system 97% cpu 0.131 total
➜ testPHP time /usr/local/opt/php70/bin/php 2.php
0.11s user 0.01s system 96% cpu 0.130 total
明显看出,快了6倍多。厉害。
二、新特性
1. 更多的标量类型声明
现在php的标量有两种模式: 强制 (默认) 和严格模式。 现在可以使用下列类型参数(无论用强制模式还是严格模式): 字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool)。它们扩充了PHP5中引入的其他类型:类名,接口,数组和 回调类型。在旧版中,函数的参数申明只能是(Array $arr)、(CLassName $obj)等,基本类型比如Int,String等是不能够被申明的。
怎么理解呢?php7之前的版本,我们要想限定一个函数的参数的类型,只有array或者class2种。
php7之前:
class MyInfo
{
public $a = 123;
public function getInfo(array $a, $b)
{
var_dump($a, $b);
}
}
function getClass(MyInfo $a) {
var_dump($a->a);
}
我们想限定 getInfo的第一个参数,必须是数组,所以,我们可以在参数$a前加一个array。来申明。
同样,我们想getClass的参数,必须是一个类,所以我们就用这个类的className前坠来申明,起到强制使用的目的。
php7之前,只有这2种标量可以使用。
我们来使用一下:
$info = new MyInfo();
$info->getInfo([1,2,3,4], 4);
我们按照规定的来,第一个参数,传数组,结果当然是正常打印:
➜ testPHP php 3.php
array(3) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
}
int(4)
要是我们不安装规定来,就会报知名错误:
$info = new MyInfo();
$info->getInfo(122, 0);
报错:
PHP Catchable fatal error: Argument 1 passed to MyInfo::getInfo() must be of the type array, integer given, called in /Users/yangyi/www/testPHP/3.php on line 25 and defined in /Users/yangyi/www/testPHP/3.php on line 8
PHP Stack trace:
PHP 1. {main}() /Users/yangyi/www/testPHP/3.php:0
PHP 2. MyInfo->getInfo() /Users/yangyi/www/testPHP/3.php:25
使用类也一样:
$info = new MyInfo();
getClass($info);
输出结果:
➜ testPHP php 3.php
int(123)
同样,我们传入别的参数,就会报错:
getClass(123);
➜ testPHP php 3.php
PHP Catchable fatal error: Argument 1 passed to getClass() must be an instance of MyInfo, integer given, called in /Users/yangyi/www/testPHP/3.php on line 27 and defined in /Users/yangyi/www/testPHP/3.php on line 17
PHP Stack trace:
PHP 1. {main}() /Users/yangyi/www/testPHP/3.php:0
PHP 2. getClass() /Users/yangyi/www/testPHP/3.php:27
我们回到这次php7的升级,它扩充了标量的类型,增加了bool、int、string、float。
php7有2种两种模式: 强制 (默认) 和严格模式。
强制模式
强制模式是默认模式,强制模式下,它会帮我们把数字类型的string类型,int整型,bool,强制类型。其他类型不能转换,就会报错。
还是刚才的例子:
class MyInfo
{
public $a = 123;
public function get1(bool $b)
{
var_dump($b);
}
public function get2(int $b)
{
var_dump($b);
}
public function get3(string $b)
{
var_dump($b);
}
public function get4(float $b)
{
var_dump($b);
}
public function get5(array $b)
{
var_dump($b);
}
}
我们先全部传入int 1
$info = new MyInfo();
$info->get1(1);
$info->get2(1);
$info->get3(1);
$info->get4(1);
看打印结果,它已经帮我们强制转换了。
➜ testPHP /usr/local/opt/php70/bin/php 3.php
/Users/yangyi/www/testPHP/3.php:11:
bool(true)
/Users/yangyi/www/testPHP/3.php:19:
int(1)
/Users/yangyi/www/testPHP/3.php:26:
string(1) "1"
/Users/yangyi/www/testPHP/3.php:33:
double(1)
我们继续,传入 string 1.23 :
$info = new MyInfo();
$info->get1('1.23');
$info->get2('1.23');
$info->get3('1.23');
$info->get4('1.23');
看下,打印结果。也已经帮我们强制转换了。
➜ testPHP /usr/local/opt/php70/bin/php 3.php
/Users/yangyi/www/testPHP/3.php:11:
bool(true)
/Users/yangyi/www/testPHP/3.php:19:
int(1)
/Users/yangyi/www/testPHP/3.php:26:
string(4) "1.23"
/Users/yangyi/www/testPHP/3.php:33:
double(1.23)
但是我们如果参数是array就没法强制转换,就会报错了:
$info->get5('1.23');
testPHP /usr/local/opt/php70/bin/php 3.php
PHP Fatal error: Uncaught TypeError: Argument 1 passed to MyInfo::get5() must be of the type array, string given, called in /Users/yangyi/www/testPHP/3.php on line 54 and defined in /Users/yangyi/www/testPHP/3.php:37
我们在PHP5.6.11运行这些代码会报错吗?试一试:
$info = new MyInfo();
$info->get1('1.23');
$info->get2('1.23');
$info->get3('1.23');
$info->get4('1.23');
➜ testPHP php 3.php
PHP Catchable fatal error: Argument 1 passed to MyInfo::get1() must be an instance of bool, string given, called in /Users/yangyi/www/testPHP/3.php on line 48 and defined in /Users/yangyi/www/testPHP/3.php on line 8
好吧。直接报错了,虽然错误提示也是说类型错误,但是,其他是不支持这些类型的申明。
严格模式
前面说了,强制模式下,它会帮我们强制转换,那么严格模式下呢?
首先,如何打开严格模式呢?
declare(strict_types=1);
加上就可以了,这样,就进入严格模式,参数必须符合规定,不然报错:
我们加上这句话,再运行下:
declare(strict_types=1);
...
...
$info = new MyInfo();
$info->get1('1.23');
$info->get2('1.23');
$info->get3('1.23');
$info->get4('1.23');
运行,看下结果,果然直接报错了。
PHP Fatal error: Uncaught TypeError: Argument 1 passed to MyInfo::get1() must be of the type boolean, string given, called in /Users/yangyi/www/testPHP/3.php on line 49 and defined in /Users/yangyi/www/testPHP/3.php:9
2. 返回值类型声明
我们知道php的函数是没有返回值类型的,return啥类型,就是啥类型。php7中增加了返回值类型,我们可以定义一个函数的返回值类型。
和php7升级的标量类型声明一样,return的类型可以是以下这些:bool、int、string、float、array、class。
举个例子来说,我们希望一个函数的返回值是一个数组,我们可以这样子书写:
:array {} // 冒号+返回类型
function returnInfo ($a) : array {
return $a;
}
var_dump(returnInfo([1,2,3]));
是不是觉得很奇怪,又无可思议!!!
查看打印结果:
➜ testPHP /usr/local/opt/php70/bin/php 3.php
/Users/yangyi/www/testPHP/3.php:64:
array(3) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
}
同样,我们想返回是int整型:
function returnInfo ($a) : int {
return $a;
}
var_dump(returnInfo('1.233'));
查看结果,他已经帮我们强制转换成整型了。
➜ testPHP /usr/local/opt/php70/bin/php 3.php
/Users/yangyi/www/testPHP/3.php:64:
int(1)
同样,我们可以返回一个class类型的:
public function getLogger(): Logger {
return $this->logger;
}
默认,也是强制模式,会帮我们转换,如果,我们想使用严格模式,同样是一样的,在文件头部加上:
declare(strict_types=1);
就可以了,这样,我们规定返回值是什么类型,就必须得是这样,不然就报致命报错。
3. null合并运算符 (??)
由于日常使用中存在大量同时使用三元表达式和 isset()的情况, php7增加了一个新的语法糖 : null合并运算符 (??)
如果变量存在且值不为NULL, 它就会返回自身的值,否则返回它的第二个操作数。
//php version = 7
$username = $user ?? 'nobody';
//php version < 7 得这样使用:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
确实方便了很多。
我记得php5.3的更新中,加入了 三元运算符简写形式:
$a ?: $b
千万别和??搞混淆了!!!
$a ?: $b的意思是 $a为true时,直接返回$a, 否则返回$b
$a ?? $b的意思是 $a isset($a)为true, 且不为NULL, 就返回$a, 否则返回$b。
看例子:
$user = 0;
$username = $user ?? 'nobody';
echo $username; //输出 0,因为 0 存在 且 不为NULL。
$username = $user ?: 'nobody';
echo $username; //输出 'nobody',因为 0 为 false
4. 太空船操作符(组合比较符)
php7 中,新加入了一个比较符号:<=> ,因为长相像太空船,所以,也叫太空船操作符。
它有啥用呢?
<=>用于比较两个表达式。当$a小于、等于或大于$b时它分别返回-1、0或1。
看例子:
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?>
其实,蛮多地方可以派上用场的。
5. 通过define()定义常量数组
Array类型的常量现在可以通过 define()来定义。在 PHP5.6 中仅能通过const定义。
在php5.3中,增加了可以使用const来申明常量,替代define()函数,但是只能申明一些简单的变量。
//旧式风格:
define("XOOO