Compile PHP to Native Binaries with TypePHP
TypePHP enables ahead-of-time (AOT) compilation of typed PHP source code into standalone, self-contained native machine binaries without embedding an entire PHP runtime.
Ahead-Of-Time Native Binaries for Modern PHP
The PHP community has taken a monumental leap forward with TypePHP, an open-source toolchain that compiles strictly typed PHP source code into native, standalone binary executables for Linux (x86_64, arm64) and macOS.
Unlike traditional tools such as Micro-PHP or Static-PHP CLI which bundle a minimized PHP C engine alongside user scripts, TypePHP translates PHP AST directly into LLVM IR. The result is zero-dependency machine code with microsecond startup times and drastically reduced memory footprints.
Key Architectural Features
- AOT Native Compilation: Eliminates opcode compilation overhead by compiling typed PHP classes into machine code.
- Zero-Dependency Distribution: Target binaries run without requiring PHP, FPM, or external extensions pre-installed on host systems.
- Strict Typing Enforcement: Leverages PHP 8.4 property types, return types, and generics syntax for aggressive optimizer passes.
Compiling Your First Binary
Here is an example of compiling a CLI utility written in PHP into a native executable:
// src/main.php
declare(strict_types=1);
namespace App;
final class Benchmark
{
public static function run(int $iterations): float
{
$start = microtime(true);
$sum = 0;
for ($i = 0; $i < $iterations; $i++) {
$sum += $i;
}
return microtime(true) - $start;
}
}
$elapsed = Benchmark::run(10000000);
echo "Executed in {$elapsed} seconds\n";
Building the executable with the TypePHP compiler CLI:
$ typephp build src/main.php --output bin/benchmark --optimize=O3
Building target: bin/benchmark [x86_64-linux-gnu]
[1/3] Parsing AST & type checking... Done.
[2/3] Generating LLVM IR & applying O3 optimization... Done.
[3/3] Linking native binary... Done (Size: 4.2 MB).
$ ./bin/benchmark
Executed in 0.00312 seconds
TypePHP opens up exciting possibilities for building high-performance CLI tools, edge workers, and lightweight microservices using familiar PHP syntax.