Can PHP Finally Be Compiled to Native Code? Inside the TypePHP AOT Compiler

TypePHP translates PHP source to C++ and then to native binaries, eliminating interpreter overhead and delivering up to 135× speed‑up on compute‑heavy benchmarks, while remaining free, open‑source, and targeting CPU‑bound workloads such as image processing, finance, and scientific calculations.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Can PHP Finally Be Compiled to Native Code? Inside the TypePHP AOT Compiler

Why PHP Is Slow

PHP’s performance issues stem from three factors: (1) it is interpreted even with OPcache, (2) every variable lives in a zval structure that requires type checks and reference‑count handling, and (3) the language’s high flexibility forces the virtual machine to handle dynamic calls and unexpected code paths. PHP 8’s JIT attempts to mitigate this but operates at runtime, often missing the short‑lived request window.

How TypePHP Works

TypePHP follows a simple AOT pipeline: PHP source → C++ → object files → native executable . It leverages existing C++ compilers (GCC, Clang, MSVC) for optimization, inlining, and vectorization, so the resulting binary contains real machine code and can run on a target without a PHP runtime.

Performance Numbers

Official benchmarks (PHP 8.4 ZendVM vs. TypePHP ‑O3) show:

fib(40) : 14.82 s → 0.11 s (≈135× faster)

1 × 10⁸ iterations of π calculation : 6.52 s → 0.094 s (≈69× faster)

The speed‑up is most pronounced for pure compute workloads that avoid I/O, because the native binary eliminates the virtual‑machine call overhead and, with the use native_types directive, replaces zval wrappers with native C++ types.

The FAQ notes that dense‑compute scenarios can see 3‑10× gains, while I/O‑bound applications typically improve only 1‑1.5×, as their bottlenecks lie outside the interpreter.

Differences from JIT

Unlike OPcache (bytecode cache) and JIT (runtime hot‑spot compilation), TypePHP performs a single compile‑time translation of all statically analyzable code, providing compile‑time type checking that surfaces errors before deployment.

Limitations and Trade‑offs

Dynamic features such as eval(), create_function(), variable variables, and dynamic function names are not supported. Projects that rely heavily on these “magic” patterns must be adapted.

Extra Features Added by TypePHP

Beyond speed, TypePHP introduces use native_types to map int, float, and bool to native C++ types, and ships high‑precision numeric types (BigInt, Decimal, BigFloat) backed by mpdecimal. It also offers strong‑typed containers (vector, map), compile‑time annotations, inline C++ code, WebAssembly output, and Python interop.

Versioning and Licensing

The underlying runtime library phpx is at v2.5.x, while the TypePHP compiler itself is at v0.4.0. The project is released under GPL, free for commercial use, but linking against libphpx may impose copyleft obligations on distributed binaries.

Deployment Simplicity

Deploying a TypePHP binary only requires the executable and libphpx.so; no PHP interpreter, Composer, or extensions are needed, making the delivery package a single file.

Quick Start Example

The following PHP program computes the 40th Fibonacci number using the native_types directive:

<?php
use native_types;
function fib(int $n): int {
    if ($n == 1 || $n == 2) {
        return 1;
    }
    return fib($n - 1) + fib($n - 2);
}
function main(int $argc, array $argv): void {
    $begin = microtime(true);
    echo fib(40) . "
";
    echo "Time: " . (microtime(true) - $begin) . "s
";
}

Compile and run with two shell commands:

$ tpc fib.php -O2 -o fib
$ ./fib

The native binary executes the benchmark in 0.11 s, matching the official measurement.

Conclusion

TypePHP does not replace PHP for typical CRUD web applications, but it opens a viable path for CPU‑bound tasks where developers can keep PHP as the source language while delivering native‑speed binaries.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performancePHPAOT compilationnative codeTypePHPJIT comparisonnative_types
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.