环境搭建

虽然php8已经上市,但是系统学习一下php7,初衷的打算是想彻底的掌握PHP的底层原理和语言结构,结合PHP开发PHP扩展、或者是编写一个Swoole的框架,解决实际生产的性能问题,解放生产力,发展生产力!

环境可以是centos ,或者是在Ubuntu系统中,对于裸镜像需要安装命令​​yum update(centos)​​,​​apt-get update;(Ubuntu)​

我的实验环境是以centos7环境。

首先进行环境搭建,用Docker搭建的centos7,php选用版本,下载php源码,比如:

#php7
https://www.php.net/distributions/php-7.4.28.tar.gz
#php5.6
https://www.php.net/distributions/php-5.6.37.tar.gz

这里要写一个小小的技巧,源码时的url是 ​​https://www.php.net/distributions/php-版本号.tar.gz​

下载PHP源码、解压、编译

wget https://www.php.net/distributions/php-7.4.28.tar.gz
wget https://www.php.net/distributions/php-5.6.37.tar.gz

解压

tar -zxvf 版本号.tar.gz

编译

--prefix=/usr/local/php7 --enable-fpm --enable-debug

编译过程中遇到了​​configure: error: no acceptable C compiler found in $PATH​​,报错信息。

编译过程中遇到了​​make: *** No targets specified and no makefile found. Stop.​​,缺少编译所需要的软件。

报错 ​​configure: error: xml2-config not found. Please check your libxml2 installation.​​,缺少编译软件。

中途遇到3次错误,原因是缺少编译依赖,执行下面依赖:

yum -y install gcc gcc-c++ autoconf \
automake zlib zlib-devel \
openssl openssl-devel \
pcre pcre-devel libxml2 \
libxml2-devel sqlite-devel -y

编译成功,执行 ​​make && make install​​。

Wrote PEAR system config file at: /usr/local/php7.1.0/etc/pear.conf
You may want to add: /usr/local/php7.1.0/lib/php to your php.ini include_path
/root/php-7.1.0/build/shtool install -c ext/phar/phar.phar /usr/local/php7.1.0/bin
ln -s -f phar.phar /usr/local/php7.1.0/bin/phar
Installing PDO headers: /usr/local/php7.1.0/include/php/ext/pdo/

PHP5、PHP7、PHP8 的性能测试对比

对比一下php5和php7的官方给出的官方性能测试Demo,5.6的版本耗时12.813s,7.1.0耗时5.122s,顺便把php8也做了一下性能测试3.780,比php7还快了一点。

[root@dd2065d03db8 php5.6]# /usr/local/php5.6/bin/php ~/php-5.6.37/Zend/bench.php
simple 0.452
simplecall 0.627
simpleucall 0.594
simpleudcall 0.680
mandel 1.177
mandel2 1.920
ackermann(7) 0.677
ary(50000) 0.154
ary2(50000) 0.087
ary3(2000) 1.265
fibo(30) 2.261
hash1(50000) 0.444
hash2(500) 0.289
heapsort(20000) 0.599
matrix(20) 0.355
nestedloop(12) 0.722
sieve(30) 0.449
strcat(200000) 0.063
------------------------
Total 12.813
[root@dd2065d03db8 php-7.1.0]# /usr/local/php7.1.0/bin/php Zend/bench.php
simple 0.182
simplecall 0.057
simpleucall 0.167
simpleudcall 0.319
mandel 0.970
mandel2 1.054
ackermann(7) 0.160
ary(50000) 0.025
ary2(50000) 0.024
ary3(2000) 0.351
fibo(30) 0.651
hash1(50000) 0.074
hash2(500) 0.064
heapsort(20000) 0.194
matrix(20) 0.245
nestedloop(12) 0.418
sieve(30) 0.127
strcat(200000) 0.040
------------------------
Total 5.122

php8

[root@dd2065d03db8 php-8.1.3]# /usr/local/php8/bin/php Zend/bench.php
simple 0.066
simplecall 0.024
simpleucall 0.117
simpleudcall 0.123
mandel 0.781
mandel2 0.777
ackermann(7) 0.135
ary(50000) 0.034
ary2(50000) 0.017
ary3(2000) 0.283
fibo(30) 0.592
hash1(50000) 0.054
hash2(500) 0.055
heapsort(20000) 0.152
matrix(20) 0.151
nestedloop(12) 0.219
sieve(30) 0.170
strcat(200000) 0.027
------------------------
Total 3.780

php7的新特性

1.太空船操作符<=>

  • 太空船操作符用于比较两个表达书
  •  PHP - php7编译安装及新特性_phpb时它分别返回-1、0或1

2.类型声明

declare(strict_types = 1); //表示脚本开启严格模式

public function receivedSent(int $toUid, string $ack ) :bool
{
//Todo 代码...
return true;
}

3.null合并操作符

$page = $_GET['page'] ?? 1;

4.NameSpace批量导入

同一个目录下的和合并使用,看下面Demo,这个特性让代码看起来更简洁

namespace App\WebSocketController\V3;

use Swoole\Websocket\Server;
use App\Utility\Ws\{Result,Category,Params,ParamsCheck};
use App\Models\{ImChatModel,ImModel};

class AdminMessage extends Base
{

}

5.throwable接口

​try..catch​​后不会直接报错,会捕捉到错误消息

object(Error)#1 (7) {
["message":protected]=>
string(38) "Call to undefined function starkName()"
["string":"Error":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(18) "/root/code/try.php"
["line":protected]=>
int(4)
["trace":"Error":private]=>
array(0) {
}
["previous":"Error":private]=>
NULL
}

6.list的括号写法

$list = [1,2];
list($a,$b) = $list;
[$a,$b] = $list;

7.抽象语法树(AST)

抽象语法树(AST) 是php7+后新增的特性