安装工具包
yum -y install libtool automake libzip-devel epel-release libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel uuid libuuid-devel gcc bzip2 bzip2-devel gmp-devel readline-devel libxslt-devel autoconf bison gcc gcc-c++ sqlite-devel cmake
下载安装 oniguruma
wget -c https://github.com/kkos/oniguruma/archive/refs/tags/v6.9.7.1.tar.gz
tar -zxvf v6.9.7.1.tar.gz
cd oniguruma-6.9.7.1
./autogen.sh && ./configure --prefix=/usr
make && make install
下载安装 php 8.1.0
wget -c https://www.php.net/distributions/php-8.1.0.tar.gz
tar -zxvf php-8.1.0.tar.gz
cd php-8.1.0
./configure \
--prefix=/opt/php/8.1.0 \
--with-config-file-path=/opt/php/8.1.0/etc \
--with-config-file-scan-dir=/opt/php/8.1.0/etc/conf.d \
--enable-fpm \
--enable-soap \
--with-openssl \
--with-openssl-dir \
--with-zlib \
--with-iconv \
--with-bz2 \
--enable-gd \
--with-jpeg \
--with-freetype \
--with-curl \
--enable-dom \
--with-xml \
--with-zip \
--enable-mbstring \
--enable-pdo \
--with-pdo-mysql \
--with-zlib-dir \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-xsl \
--enable-mysqlnd \
--with-mysqli \
--without-pear \
--disable-short-tags
make && make install
Installing shared extensions: /opt/php/8.1.0/lib/php/extensions/no-debug-non-zts-20210902/
Installing PHP CLI binary: /opt/php/8.1.0/bin/
Installing PHP CLI man page: /opt/php/8.1.0/php/man/man1/
Installing PHP FPM binary: /opt/php/8.1.0/sbin/
Installing PHP FPM defconfig: /opt/php/8.1.0/etc/
Installing PHP FPM man page: /opt/php/8.1.0/php/man/man8/
Installing PHP FPM status page: /opt/php/8.1.0/php/php/fpm/
Installing phpdbg binary: /opt/php/8.1.0/bin/
Installing phpdbg man page: /opt/php/8.1.0/php/man/man1/
Installing PHP CGI binary: /opt/php/8.1.0/bin/
Installing PHP CGI man page: /opt/php/8.1.0/php/man/man1/
Installing build environment: /opt/php/8.1.0/lib/php/build/
Installing header files: /opt/php/8.1.0/include/php/
Installing helper programs: /opt/php/8.1.0/bin/
program: phpize
program: php-config
Installing man pages: /opt/php/8.1.0/php/man/man1/
page: phpize.1
page: php-config.1
/home/centos/php-8.1.0/build/shtool install -c ext/phar/phar.phar /opt/php/8.1.0/bin/phar.phar
ln -s -f phar.phar /opt/php/8.1.0/bin/phar
Installing PDO headers: /opt/php/8.1.0/include/php/ext/pdo/
启动和后续配件可以参考这里
Centos7 编译 PHP7.4.16 - 简书 (jianshu.com)
增加扩展
进入刚才的解压目录
#安装GD库示例
cd php-8.1.0/ext/gd
/opt/php/8.1.0/bin/phpize
Configuring for:
PHP Api Version: 20210902
Zend Module Api No: 20210902
Zend Extension Api No: 420210902
./configure --with-php-config=/opt/php/8.1.0/bin/php-config
make && make install
完成后记得在 php.ini 里加入
extension=gd.so
PHP8 主要不向后兼容的变更
- 字符串与数字的比较
数字与非数字形式的字符串之间的非严格比较现在将首先将数字转为字符串,然后比较这两个字符串。
数字与数字形式的字符串之间的比较仍然像之前那样进行。
请注意,这意味着 0 == "not-a-number" 现在将被认为是 false 。
Comparison | Before | After |
---|---|---|
0 == "0" | true | true |
0 == "0.0" | true | true |
0 == "foo" | true | false |
0 == "" | true | false |
42 == " 42" | true | true |
42 == "42foo" | true | false |
match
现在是一个保留字。断言(Assertion)失败现在默认抛出异常。如果想要改回之前的行为,可以在 INI 设置中设置
assert.exception=0
。与类名相同的方法名将不再被当做构造方法。应该使用__construct() 来取代它。
不再允许通过静态调用的方式去调用非静态方法。因此is_callable()在检查一个类名与非静态方法 时将返回失败(应当检查一个类的实例)。
(real)
和(unset)
转换已被移除
PHP8 主要新特性
命名参数
新增命名参数的功能。
注解(Attributes)
新增注解的功能。
构造器属性提升(Constructor Property Promotion)
新增构造器属性提升功能 在构造函数中声明类的属性)。
联合类型
新增 联合类型。
Match 表达式
新增 match
表达式。
Nullsafe 运算符
新增Nullsafe 运算符(?->
)。
其他新特性
新增 WeakMap 类。
新增 ValueError 类。
现在,只要类型兼容,任意数量的函数参数都可以用一个可变参数替换。 例如允许编写下面的代码:
<?php class A {
public function method(int $many, string $parameters, $here) {}
}
class B extends A {
public function method(...$everything) {}
} ?>
- static ("后期静态绑定"中) 可以作为返回类型:
<?php class Test {
public function create(): static {
return new static();
}
} ?>
现在可以通过
$object::class
获取类名,返回的结果和get_class($object)
一致。new
、instanceof
可用于任何表达式, 用法为new (expression)(...$args)
和$obj instanceof (expression)
。添加对一些变量语法一致性的修复,例如现在能够编写
Foo::BAR::$baz
。添加 Stringable interface, 当一个类定义 __toString() 方法后会自动实现该接口。
Trait 可以定义私有抽象方法(abstract private method)。 类必须实现 trait 定义的该方法。
可作为表达式使用
throw
。 使得可以编写以下用法:
<?php
$fn = fn() => throw new Exception('Exception in arrow function'); $user = $session->user ?? throw new Exception('Must have user');
- 参数列表中的末尾逗号为可选。
<?php function functionWithLongSignature(
Type1 $parameter1,
Type2 $parameter2, // <-- 这个逗号也被允许了 ) {
}
现在允许
catch (Exception)
一个 exception 而无需捕获到变量中。支持 mixed 类型。