原文提到的 PHP 6.0 因 Unicode 實作困難、開發停滯,最終於 2010 年決定跳過 6.0 直接發布 PHP 7.0。PHP 6 的部分特性 (如 `intl` 擴充套件) 移植至 5.3+。本文重新梳理 PHP 5.6 至 8.x 的關鍵演進,作為遷移參考。
| 版本 | 發布時間 | 狀態 | 核心主題 |
|---|---|---|---|
| 5.6 | 2014-08 | EOL 2018-12 | 常數陣列、可變參數 `...`、phpdbg、OpenSSL 改進 |
| 7.0 | 2015-12 | EOL 2018-12 | Zend Engine 3、效能提升 2x、標量/回傳型別宣告、?? 空合併運算子、太空船運算子 <=>、Anonymous Class、Group Use Declarations |
| 7.1 | 2016-12 | EOL 2019-12 | 可為 Null 型別 `?Type`、void 回傳、類別常數可見性、`list()` 指定鍵名、多重 catch |
| 7.2 | 2017-11 | EOL 2020-11 | 物件型別 `object`、參數擴展 `...`、Argon2 密碼雜湊、Libsodium、擴充套件載入機制 |
| 7.3 | 2018-12 | EOL 2021-12 | Heredoc/Nowdoc 縮排、JSON_THROW_ON_ERROR、list() 參考賦值、PCRE2、hrtime() |
| 7.4 | 2019-11 | 安全維護至 2024-11 | 箭頭函式 `fn()`、類型屬性、Nullsafe `?->`、預載入、擴散運算子陣列、FFI、自訂序列化 |
| 8.0 | 2020-11 | 安全維護至 2025-11 | JIT 編譯、Union Types、Named Arguments、Attributes (屬性)、Match Expression、Constructor Promotion、Nullsafe Operator、Weak Maps、Stringable 介面、throw Expression |
| 8.1 | 2021-11 | 安全維護至 2026-11 | Enum、Fibers (纖維/協程)、readonly 屬性、First-class Callable、`never` 回傳、Final 常數、Array Unpacking 字串鍵 |
| 8.2 | 2022-12 | 安全維護至 2027-12 | Readonly Class、DNF Types、隨機擴充 `Randomizer`、常數 Trait、敏感參數標記 |
| 8.3 | 2023-11 | 安全維護至 2028-11 | Typed Class Constants、Dynamic `fetch_class`、JSON 驗證錯誤、Random 介面、`#[Override]` 屬性 |
| 8.4 | 2025-06 (預計) | 開發中 | Property Hooks、非對稱可見性、HTML5 解析器、性能優化 |
| PHP 6 提議特性 | 現今實現版本/方式 | 備註 |
|---|---|---|
| Unicode 核心支援 | PHP 7.0+ (內建 UTF-8 字串處理) | 非語言層面內建,但 mbstring/intl/ICU 提供完整支援 |
| Namespaces (命名空間) | PHP 5.3 (2009) | 已普及,PSR-4 自動載入標準 |
| SOAP | PHP 5.0+ (ext/soap) | 現代多用 REST/GraphQL/gRPC |
| XMLReader/XMLWriter | PHP 5.1+ (內建) | 配合 DOMDocument、SimpleXML |
| 移除 magic_quotes | PHP 5.4 移除 | 已移除,歷史遺留問題 |
| 移除 register_globals | PHP 5.4 移除 | 已移除,安全性重大改進 |
| 移除 register_long_arrays | PHP 5.4 移除 | 已移除 |
| 移除 safe_mode | PHP 5.4 移除 | 已移除,改用 open_basedir、容器隔離 |
# 版本管理
phpbrew / php-build / asdf-php / docker
# 靜態分析 (CI 必裝)
composer require --dev phpstan/phpstan psalm/plugin-phpstan
# phpstan analyse src --level=5
# 代碼風格
composer require --dev friendsofphp/php-cs-fixer
# php-cs-fixer fix --rules=@PSR12
# 測試
composer require --dev phpunit/phpunit pestphp/pest
# 依賴管理
composer require symfony/console doctrine/orm laminas/laminas-db
# 部署
# PHP-FPM + Nginx (傳統)
# RoadRunner / FrankenPHP / Swoole (高效能長駐)
# Kubernetes + PHP-FPM / FrankenPHP
phpcs --standard=PHPCompatibility、rector (自動重構)<?php
// PHP 8.0+ 現代語法展示
// 1. Constructor Property Promotion + Readonly (8.1)
readonly class UserDto {
public function __construct(
public int $id,
public string $name,
public string $email,
public DateTimeImmutable $createdAt,
) {}
}
// 2. Enum (8.1)
enum OrderStatus: string {
case Pending = 'pending';
case Paid = 'paid';
case Shipped = 'shipped';
case Cancelled = 'cancelled';
}
// 3. Match Expression (8.0) - 替代 switch
$message = match ($status) {
OrderStatus::Pending => '等待付款',
OrderStatus::Paid => '已付款,備貨中',
OrderStatus::Shipped => '已出貨',
OrderStatus::Cancelled => '訂單取消',
};
// 4. Attributes (8.0) - 替代註解
#[Route('/api/users/{id}', methods: ['GET'])]
#[Validate(['id' => 'required|integer|min:1'])]
class UserController {
public function show(#[MapFrom('id')] int $id): UserDto { /* ... */ }
}
// 5. Nullsafe Operator (8.0)
$country = $user?->address?->country?->name;
// 6. Union Types (8.0) + DNF Types (8.2)
function process(InputInterface|string|array $input): (UserDto|ErrorDto)&Countable { /* ... */ }
// 7. Fibers (8.1) - 協程基礎
$fiber = new Fiber(function (): void {
$value = Fiber::suspend('hello');
echo "Resumed with: $value";
});
$value = $fiber->start();
$fiber->resume('world');
// 8. Randomizer (8.2) - 現代隨機數
$rng = new Random\Randomizer();
$password = $rng->getBytes(32);
$int = $rng->getInt(1, 100);
$shuffled = $rng->shuffle([1,2,3,4,5]);
PHP 6 雖未問世,但其預想特性多在後續版本落地。PHP 7.0 的效能飛躍、PHP 8.0 的 JIT 與現代語法、PHP 8.1+ 的 Enum/Fibers/Readonly,已將 PHP 打造為具備企業級開發能力的現代語言。建議專案最低鎖定 PHP 8.1 (長期支援至 2026),漸進遷移至 8.3 享受最新特性與安全更新。