Compile PHP to Native Binaries with TypePHP
Open Source PHP #TypePHP #AOT Compiler #PHP Performance #Swoole #Native Binaries #PHP Tooling

Compile PHP to Native Binaries with TypePHP

3 min read Mohamed Said Mohamed Said

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:

  1. PHP source → C++ — TypePHP parses your PHP code and emits equivalent C++ source.
  2. C++ → native binary — The generated C++ is compiled down to a native machine-code binary that runs directly on the CPU.
  3. 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

Found this useful?

Frequently Asked Questions

3 questions
Q01 What is TypePHP and how does it differ from OPcache?
TypePHP is an Ahead-Of-Time (AOT) compiler that translates PHP source code into C++ and then into native machine code. Unlike OPcache, which caches Zend opcodes that are still interpreted at runtime, TypePHP generates native binaries that run directly on the CPU without any opcode interpretation.
Q02 Does TypePHP break compatibility with existing PHP extensions and dynamic features?
No. Dynamic PHP values, internal functions, reflection, and object metadata continue to interoperate with the Zend runtime through PHPX, so existing libraries and dynamic language features are not lost after compilation.
Q03 Is TypePHP written in PHP or does it require a C/C++ toolchain to develop?
TypePHP is written entirely in PHP and is fully self-hosting — the tpc compiler binary is built by compiling the compiler's own PHP source with TypePHP. There is no C or C++ glue code inside the compiler itself, though a C++ compiler is needed to compile the generated output.

Continue reading

More Articles

View all