PHP 简介
PHP 8.0 是 PHP 语言的一个重大更新版本,最新稳定版本是 PHP 8.4.1 ,于 2024 年 11 月 21 日发布。它引入了多项新特性和性能改进,同时也修复了旧版本中的一些 bug。以下是 PHP 8.0 带来的一些主要更新和特性:
- JIT 编译器(Just-In-Time Compiler):
- 引入了名为 Tracing JIT 的即时编译器,这使得某些代码段的性能显著提升,特别是对于 CPU 密集型的应用。
- 命名参数(Named Arguments):
- 允许在函数调用时通过参数名称指定参数值,无需遵循函数定义中的顺序。
- 联合类型(Union Types):
- 支持在类型声明中使用多个类型,例如
?string
或int|string
。
- 支持在类型声明中使用多个类型,例如
- 构造器属性提升(Constructor Property Promotion):
- 可以直接在构造函数中定义和初始化类的属性,简化了类的定义。
- Match 表达式:
- 类似于 switch 语句但更强大,支持模式匹配。
- Nullsafe 运算符:
- 引入了一个新的运算符
?->
,允许在对象链上调用方法或访问属性而不用担心中间出现 null 值。
- 引入了一个新的运算符
- 弱映射(Weak Maps):
- 扩展了自 PHP 7.4 中引入的弱引用的概念,允许创建从对象到数据的映射而不增加对象的引用计数。
- 静态返回类型(Static Return Type):
- 函数可以声明返回 static 类型,即返回调用该方法的对象实例类型。
- 改进的类型系统:
- 包括对属性的支持、更严格的类型检查等。
- 废弃和移除功能:
- 移除了过时的功能和扩展,如 MySQL 扩展,鼓励使用 MySQLi 或 PDO。
在 Rocky Linux 上启用 Remi 存储库
温馨提示
本教程基于 Rocky Linux 8.10 版本部署。
因为 PHP 8 尚未在 Rocky Linux AppStream 存储库中提供。所以,我们将从 Remi 存储库安装 PHP 8.4.1 ,这是一个提供 PHP 堆栈的免费第三方 YUM 存储库。
[root@localhost ~]# cat /etc/redhat-release
Rocky Linux release 8.10 (Green Obsidian)
[root@localhost ~]# dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Last metadata expiration check: 2:05:26 ago on Tue 17 Dec 2024 03:25:39 PM CST.
remi-release-8.rpm 35 kB/s | 35 kB 00:00
Dependencies resolved.
=========================================================================================================================================================
Package Architecture Version Repository Size
=========================================================================================================================================================
Installing:
remi-release noarch 8.10-1.el8.remi @commandline 35 k
yum-utils noarch 4.0.21-25.el8 baseos 75 k
Transaction Summary
=========================================================================================================================================================
Install 2 Packages
Total size: 109 k
Total download size: 75 k
Installed size: 53 k
Is this ok [y/N]: y
Downloading Packages:
yum-utils-4.0.21-25.el8.noarch.rpm 409 kB/s | 75 kB 00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------
Total 402 kB/s | 75 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : remi-release-8.10-1.el8.remi.noarch 1/2
Installing : yum-utils-4.0.21-25.el8.noarch 2/2
Running scriptlet: yum-utils-4.0.21-25.el8.noarch 2/2
Verifying : yum-utils-4.0.21-25.el8.noarch 1/2
Verifying : remi-release-8.10-1.el8.remi.noarch 2/2
Installed:
remi-release-8.10-1.el8.remi.noarch yum-utils-4.0.21-25.el8.noarch
Complete!
启用 PHP Remi 存储库
查看默认的模块列表,PHP 7.2 是默认安装的模块版本。如果我们直接进行安装 PHP,将默认选择 PHP 7.2 版本,所以我们需要重置模块版本,启用最新的存储库版本。
[root@localhost ~]# dnf module list php
Last metadata expiration check: 0:01:18 ago on Tue 17 Dec 2024 05:38:56 PM CST.
Rocky Linux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
php 8.0 common [d], devel, minimal PHP scripting language
php 8.2 common [d], devel, minimal PHP scripting language
Remi''s Modular repository for Enterprise Linux 8 - x86_64
Name Stream Profiles Summary
php remi-7.2 common [d], devel, minimal PHP scripting language
php remi-7.3 common [d], devel, minimal PHP scripting language
php remi-7.4 common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
php remi-8.1 common [d], devel, minimal PHP scripting language
php remi-8.2 common [d], devel, minimal PHP scripting language
php remi-8.3 common [d], devel, minimal PHP scripting language
php remi-8.4 common [d], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
[root@localhost ~]# dnf install php
Last metadata expiration check: 0:05:16 ago on Tue 17 Dec 2024 05:38:56 PM CST.
Dependencies resolved.
=========================================================================================================================================================
Package Architecture Version Repository Size
=========================================================================================================================================================
Installing:
php x86_64 7.2.24-1.module+el8.4.0+413+c9202dda appstream 1.5 M
Installing dependencies:
httpd x86_64 2.4.37-65.module+el8.10.0+1830+22f0c9e0 appstream 1.4 M
httpd-filesystem noarch 2.4.37-65.module+el8.10.0+1830+22f0c9e0 appstream 43 k
mailcap noarch 2.1.48-3.el8 baseos 38 k
mod_http2 x86_64 1.15.7-10.module+el8.10.0+1883+38ed6c58.1 appstream 155 k
nginx-filesystem noarch 1:1.14.1-9.module+el8.4.0+542+81547229 appstream 23 k
php-cli x86_64 7.2.24-1.module+el8.4.0+413+c9202dda appstream 3.1 M
php-common x86_64 7.2.24-1.module+el8.4.0+413+c9202dda appstream 660 k
rocky-logos-httpd noarch 86.3-1.el8 baseos 24 k
Installing weak dependencies:
php-fpm x86_64 7.2.24-1.module+el8.4.0+413+c9202dda appstream 1.6 M
Enabling module streams:
nginx 1.14
php 7.2
Transaction Summary
=========================================================================================================================================================
Install 10 Packages
Total download size: 8.6 M
Installed size: 30 M
Is this ok [y/N]:
我们需要安装最新版本:PHP 8.4.1 我们先重置 PHP 默认版本,再启用 PHP 8 版本。
[root@localhost ~]# dnf module reset php
Last metadata expiration check: 0:09:18 ago on Tue 17 Dec 2024 05:38:56 PM CST.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost ~]# dnf module enable php:remi-8.0
Last metadata expiration check: 0:09:58 ago on Tue 17 Dec 2024 05:38:56 PM CST.
Dependencies resolved.
=========================================================================================================================================================
Package Architecture Version Repository Size
=========================================================================================================================================================
Enabling module streams:
php remi-8.0
Transaction Summary
=========================================================================================================================================================
Is this ok [y/N]: y
Complete!
安装 PHP 8.4.1
我们根据需要安装对应的 PHP 功能模块。
[root@localhost ~]# dnf install php php-cli php-curl php-mysqlnd php-gd php-opcache php-zip php-intl
Last metadata expiration check: 0:14:54 ago on Tue 17 Dec 2024 05:38:56 PM CST.
Dependencies resolved.
=========================================================================================================================================================
Package Architecture Version Repository Size
=========================================================================================================================================================
Installing:
php x86_64 8.0.30-11.el8.remi remi-modular 1.6 M
php-cli x86_64 8.0.30-11.el8.remi remi-modular 4.7 M
php-common x86_64 8.0.30-11.el8.remi remi-modular 1.2 M
php-gd x86_64 8.0.30-11.el8.remi remi-modular 108 k
php-intl x86_64 8.0.30-11.el8.remi remi-modular 246 k
php-mysqlnd x86_64 8.0.30-11.el8.remi remi-modular 270 k
php-opcache x86_64 8.0.30-11.el8.remi remi-modular 785 k
php-pecl-zip x86_64 1.22.4-1.el8.remi.8.0 remi-modular 75 k
Installing dependencies:
dejavu-fonts-common noarch 2.35-7.el8 baseos 73 k
dejavu-sans-fonts noarch 2.35-7.el8 baseos 1.5 M
fontconfig x86_64 2.13.1-4.el8 baseos 273 k
fontpackages-filesystem noarch 1.44-22.el8 baseos 15 k
fribidi x86_64 1.0.4-9.el8 appstream 88 k
gd3php x86_64 2.3.3-8.el8.remi remi-safe 148 k
graphite2 x86_64 1.3.10-10.el8 appstream 120 k
harfbuzz x86_64 1.7.5-4.el8 appstream 295 k
httpd x86_64 2.4.37-65.module+el8.10.0+1830+22f0c9e0 appstream 1.4 M
httpd-filesystem noarch 2.4.37-65.module+el8.10.0+1830+22f0c9e0 appstream 43 k
jbigkit-libs x86_64 2.1-14.el8 appstream 54 k
libX11 x86_64 1.6.8-9.el8_10 appstream 611 k
libX11-common noarch 1.6.8-9.el8_10 appstream 157 k
libXau x86_64 1.0.9-3.el8 appstream 36 k
libXpm x86_64 3.5.12-11.el8 appstream 58 k
libaom x86_64 3.6.1-1.el8 epel 1.9 M
libavif x86_64 0.10.1-3.el8 epel 76 k
libdav1d x86_64 0.5.2-2.el8 epel 346 k
libicu74 x86_64 74.2-1.el8.remi remi-safe 10 M
libimagequant x86_64 2.12.5-1.el8 epel 63 k
libjpeg-turbo x86_64 1.5.3-12.el8 appstream 156 k
libraqm x86_64 0.7.0-4.el8 epel 19 k
libsodium x86_64 1.0.18-2.el8 epel 162 k
libtiff x86_64 4.0.9-33.el8_10 appstream 189 k
libwebp x86_64 1.0.0-10.el8 appstream 273 k
libxcb x86_64 1.13.1-1.el8 appstream 228 k
libxslt x86_64 1.1.32-6.el8 baseos 249 k
libzip x86_64 1.11.2-1.el8.remi remi-modular 74 k
mailcap noarch 2.1.48-3.el8 baseos 38 k
mod_http2 x86_64 1.15.7-10.module+el8.10.0+1883+38ed6c58.1 appstream 155 k
oniguruma5php x86_64 6.9.9-2.el8.remi remi-safe 213 k
php-pdo x86_64 8.0.30-11.el8.remi remi-modular 162 k
rocky-logos-httpd noarch 86.3-1.el8 baseos 24 k
svt-av1-libs x86_64 0.8.7-1.el8 epel 4.7 M
Installing weak dependencies:
nginx-filesystem noarch 1:1.14.1-9.module+el8.4.0+542+81547229 appstream 23 k
php-fpm x86_64 8.0.30-11.el8.remi remi-modular 1.6 M
php-mbstring x86_64 8.0.30-11.el8.remi remi-modular 530 k
php-sodium x86_64 8.0.30-11.el8.remi remi-modular 100 k
php-xml x86_64 8.0.30-11.el8.remi remi-modular 246 k
Enabling module streams:
nginx 1.14
Transaction Summary
=========================================================================================================================================================
Install 47 Packages
Total download size: 35 M
Installed size: 124 M
Is this ok [y/N]:
安装完成后验证 PHP 版本是否正确,这种安装方式会自动安装 PHP-FPM 管理工具,PHP-FPM 是 PHP FastCGI Process Manager 的缩写,是一个 PHP FastCGI 管理器,用于接收 Web 服务器传递过来的请求,处理并返回动态页面内容。
[root@localhost ~]# php -v
PHP 8.4.1 (cli) (built: Nov 20 2024 09:48:35) (NTS gcc x86_64)
Copyright (c) The PHP Group
Built by Remi''s RPM repository <https://rpms.remirepo.net/> #StandWithUkraine
Zend Engine v4.4.1, Copyright (c) Zend Technologies
with Zend OPcache v8.4.1, Copyright (c), by Zend Technologies
参考文献
[1] PHP8.4官方文档
版权声明:「自由转载-保持署名-非商业性使用-禁止演绎 3.0 国际」(CC BY-NC-ND 3.0)
用一杯咖啡支持我们,我们的每一篇[文档]都经过实际操作和精心打磨,而不是简单地从网上复制粘贴。期间投入了大量心血,只为能够真正帮助到您。
暂无评论