TypePHP: An AOT Compiler for PHP
The Swoole team has open-sourced TypePHP, an Ahead-Of-Time (AOT) compiler that translates PHP source code into native machine code. The project was originally slated for an October release but shipped earlier than planned, as noted by Albert Chen on X.
How TypePHP Works
TypePHP takes a fundamentally different approach from the tools most PHP developers are used to:
- PHP source → C++ — TypePHP parses your PHP code and emits equivalent C++ source.
- C++ → native binary — The generated C++ is compiled down to a native machine-code binary that runs directly on the CPU.
- No opcode interpretation at runtime — Unlike OPcache or a traditional PHP VM, there are no Zend opcodes being interpreted once a function has been compiled.
PHP source code
│
▼
TypePHP (tpc)
│
▼
C++ source
│
▼
Native binary
Familiar Syntax, Compile-Time Types
TypePHP keeps standard PHP syntax intact and layers on compile-time type information. This allows the compiler to emit fast, statically-typed C++ for hot paths. Dynamic PHP values, internal functions, reflection, and object metadata continue to interoperate with the Zend runtime through PHPX, so you are not losing access to the broader PHP ecosystem.
Fully Self-Hosting
One of the more remarkable aspects of TypePHP is that it is written entirely in PHP and is fully self-hosting. The tpc compiler binary is produced by compiling the compiler's own PHP source code with TypePHP itself. The entire bootstrap chain is pure PHP — there is no C or C++ glue code inside the compiler.
What This Means for PHP Developers
For Laravel and PHP developers, TypePHP represents a significant shift in what is possible with the language:
- Performance ceiling rises — Native binaries eliminate interpreter overhead for compiled functions.
- Distribution simplifies — A self-contained binary can be shipped without requiring a PHP runtime on the target machine.
- Type discipline pays off — Code with explicit type annotations gives the compiler more information to generate optimised C++.
- Zend interop is preserved — Dynamic features, extensions, and reflection still work through the PHPX bridge, so existing libraries are not immediately stranded.
- Pure-PHP toolchain — No C/C++ knowledge is required to work with or contribute to the compiler itself.
Getting Started
The project is available on GitHub under the Swoole organisation. Full documentation and additional details are on the TypePHP site.
# Clone the repository
git clone https://github.com/swoole/typephp
Refer to the official docs for build instructions, supported PHP syntax, and interoperability notes before using TypePHP in a production context.
Key Takeaways
- TypePHP is an AOT compiler that converts PHP → C++ → native machine code.
- Compiled functions bypass Zend opcode interpretation entirely.
- Compile-time type information drives C++ code generation for hot paths.
- Dynamic PHP features continue to work via the PHPX Zend runtime bridge.
- The compiler is self-hosting and written entirely in PHP.
- The project was released early by the Swoole team ahead of its planned October launch.
Source: Laravel News — Compile PHP to Native Binaries with TypePHP