
PHP 长期作为解释型语言,在性能上难以与 Rust、Go 等编译型语言竞争。
Swoole-Compiler v4 推出的 Native AOT(Ahead-of-Time)编译器 改变了这一现状。
它能将 PHP 代码直接编译为原生二进制可执行文件,性能相比传统 PHP 解释器提升可达上百倍,达到与 Rust、Golang 相当的水平。
核心特点:
参考文章:Swoole-Compiler 将提供 PHP Native AOT 编译器,支持将 PHP 代码编译为可执行文件,运算性能提高 150 倍
与 HHVM、KPHP 不同,Swoole AOT 不是 PHP 的另一种实现。它使用 ZendPHP 底层库 + PHPX 兼容层,直接将 PHP 代码编译为 C++ 风格的机器指令,运行机制与 C++/Go 一致。

D:\workspace\swoole-compiler-windows-x64PHP_HOME=D:\workspace\swoole-compiler-windows-x64
PHPX_HOME=D:\workspace\swoole-compiler-windows-x64\phpx
# 将以下路径添加到 Path
D:\workspace\swoole-compiler-windows-x64
提示:软件包内已包含完整 PHP 8.4 ZTS,可通过 php.ini 加载扩展。
hello.php<?php
declare(strict_types=1);
function main(): void
{
echo "Hello Tinywan ".PHP_EOL;
var_dump(PHP_VERSION);
var_dump(php_uname());
global $argv;
var_dump($argv);
}
cd D:\workspace\swoole-compiler-windows-x64
.\swoole_compiler.exe .\hello.php
正常输出
Initialized platform/backend: Windows + MSVC (cl)
prepare: hello.php
prepare completed: 1 source files in total
convert: hello.php
generate arginfo file: hello.php
Starting compilation for5 files
cl /c "D:\workspace\swoole-compiler-windows-x64/build\\hello.cc"
hello.cc
cl /c "D:\workspace\swoole-compiler-windows-x64/build/extension-app_hello.cc"
extension-app_hello.cc
cl /c "D:\workspace\swoole-compiler-windows-x64\phpx/src/misc/main.cc"
main.cc
cl /c "D:\workspace\swoole-compiler-windows-x64\phpx/src/misc/php_cli_process_title.c" o
php_cli_process_title.c
ps_title.c
Successfully compiled 5 files
Build successful: hello.exe
编译成功后会生成 hello.exe(不包含 PHP 源码,直接是机器指令)。
D:\workspace\swoole-compiler-windows-x64>.\hello.exe
Hello Tinywan
string(6) "8.4.20"
string(51) "Windows NT TINYWAN 6.2 build 9200 (Windows 8) AMD64"
array(1) {
[0]=>
string(11) ".\hello.exe"
}
错误现象:
Fatal error: compile failed: D:\...\build\\hello.cc
#0 [internal function]: PhpAot\Php\Translator->compile(Array)
解决方法:
在编译前先初始化 Visual Studio 命令行环境:
# 进入 swoole-compiler 目录
cd D:\workspace\swoole-compiler-windows-x64
# 执行 vcvars(路径根据你的 VS 安装版本调整)
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
**********************************************************************
** Visual Studio 2026 Developer Command Prompt v18.6.0
** Copyright (c) 2026 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
然后再执行编译命令:
.\swoole_compiler.exe .\hello.php
推荐做法:每次新开 PowerShell 窗口都先执行 vcvars64.bat,或直接使用 Visual Studio Developer Command Prompt。
.cc 等),后续修改 PHP 代码后需重新编译。