TypePHP: The AOT Compiler That Turns PHP Into Native Machine Code

TypePHP: The AOT Compiler That Turns PHP Into Native Machine Code
Blog 3 Min. read
TypePHP is an Ahead-of-Time compiler from the Swoole team that translates PHP source into C++17, then into native machine code. Benchmarks show 6–8× faster execution than standard PHP.

What Is TypePHP?

TypePHP is an Ahead-of-Time (AOT) compiler developed by the Swoole team. It translates PHP source code into C++17, which is then compiled into native machine code that runs directly on the CPU. Unlike a bytecode cache or a JIT compiler, TypePHP does not interpret opcodes at runtime — compilation is complete before the program runs.

It keeps PHP's familiar syntax and adds compile-time type information so the compiler can emit fast, statically-typed C++ for hot paths.

How It Works

PHP source + .stub.php declarations
             ↓
   Parse, validate, and collect declarations
             ↓
   Lower function bodies to C++17
             ↓
   Native compiler → executable | PHP extension | shared library

Benchmark: How Much Faster?

Running the official PHP language benchmarks (bench.php and micro_bench.php) from the PHP source tree, compiled with -O3:

  • bench.php: PHP 5.034s → TypePHP 0.603s — roughly 8× faster
  • micro_bench.php: PHP 13.045s → TypePHP 2.021s — roughly 6.5× faster

For arrays: a 10,000 × 100,000 element update loop shows std::array (TypePHP AOT) at approximately 10× faster than PHP arrays (even with JIT) and on par with C++ std::vector.

Three Build Modes

  • bin (default): Standalone native executable — no PHP CLI required at runtime
  • ext: Loadable PHP extension — adds compiled functions/classes to an existing PHP SAPI
  • lib: Shared library — reusable compiled TypePHP API callable from other projects

Key Features

  • Native type system: int, float, and bool map directly to C++ scalar types (int64_t, double, bool). Arithmetic compiles to plain CPU instructions.
  • Strongly-typed containers: std::array, std::vector, std::map with compile-time element types — up to 10× faster than PHP arrays.
  • High-precision numerics: bigInt (GMP), decimal (libmpdec), bigFloat (MPFR) — exact arithmetic without overflow.
  • Mixed C++/PHP: Write performance-critical kernels in C++ and call them directly from PHP.
  • Cross-platform: Linux, Windows, macOS; x64 and ARM64; plus WASM output.
  • Source protection: Ship native binaries instead of readable PHP files.

Limitations: Not All PHP Code Can Be Compiled

  • Global scope is declaration-only — executable statements must be inside functions
  • Binary mode requires a strict main() entry point
  • Some dynamic reflection, closure, and reference patterns are unsupported
  • Existing PHP applications cannot be dropped in directly — check the incompatible-feature list first

Who Should Use It?

TypePHP is currently at v0.7.0 (pre-1.0) and under active development. Good fits today:

  • Numerically intensive CLI tools
  • Commercial PHP applications where source protection is required
  • Libraries where near-native performance is needed with PHP syntax

For large existing web applications, it is still early. But given the development pace, it could become a serious production tool within the next 1–2 years. Source: github.com/swoole/typephp.

Looking for reliable hosting for PHP applications? Check our cloud servers and web hosting plans.

How is TypePHP different from PHP's built-in JIT compiler?

PHP's JIT optimises frequently-run code paths at runtime and still operates inside the Zend VM, requiring a warm-up period. TypePHP compiles ahead of time — there is no interpreter or PHP VM at runtime. The result is far more predictable and deterministic performance compared to JIT.

Can I compile an existing Laravel application with TypePHP?

Not yet. TypePHP does not support the full range of dynamic PHP features, and executable code in global scope is not allowed. Laravel's bootstrap layer conflicts with these restrictions. Today's target audience is new, statically-structured projects and CLI tools written in PHP syntax. Framework support may come as the project matures.

Is TypePHP independent from Swoole the async framework?

Yes. TypePHP is a separate project built by the Swoole team but has no runtime dependency on Swoole's async/coroutine framework. In binary mode the compiled executable runs directly on the CPU without even a PHP CLI installed. Extension mode uses the Zend runtime, but Swoole itself is not required.

TypePHP AOT compiler Swoole PHP native PHP performance

EuroVDC

Host Your Website with Confidence

NVMe SSD storage · Free SSL · cPanel included

View Web Hosting Plans

Did you find this content useful?

People found it useful

Share on Social Media

TypePHP: The AOT Compiler That Turns PHP Into Native Machine Code