Overview
TypePHP is a groundbreaking Ahead-Of-Time (AOT) compiler that revolutionizes PHP development by transforming your familiar PHP code into highly optimized native machine code. It goes beyond traditional bytecode caching and virtual machines, producing standalone executables, efficient PHP extensions, and dynamic shared libraries that run directly on the CPU without interpretation. This approach unlocks near-native performance for your PHP applications while retaining the syntax and flexibility you already know and love.
Key Features
- True AOT Compilation: Compiles PHP directly to C++ and then to native machine code, eliminating runtime interpretation and JIT warm-up delays.
- Self-Hosting Compiler: Written entirely in PHP, the TypePHP compiler (
tpc) bootstraps itself by compiling its own source code. - Multiple Output Formats: Generate native executables (
bin), loadable PHP extensions (ext), or reusable shared libraries (lib) from a single codebase. - Native Type System Integration: Seamlessly maps PHP scalars (
int,float,bool) to their C++ counterparts for significant performance gains, especially in numeric computations. - High-Precision Numerics & Typed Containers: Leverages
bigInt(GMP),decimal(libmpdec), andbigFloat(MPFR), along with strongly-typed containers likestd::vectorandstd::map, offering up to 10x speedups over standard PHP arrays. - Universal Method Calls: Invoke methods directly on primitive types (e.g.,
$s->upper()), with statically known calls resolved at compile time for maximum efficiency. - Compile-Time Safety & Code Generation: Features compile-time attributes like
#[Immutable]and#[ArrayDef]for enhanced safety, along with code generation attributes (#[Getter],#[Setter],#[With], etc.) for boilerplate reduction. - Modern PHP Support: Fully compatible with the latest PHP features, including property hooks, asymmetric visibility, and discard expressions.
- Cross-Platform & WASM: Supports Linux, Windows, and macOS targets (x64 and ARM64), with WASI and browser output capabilities.
- Gradual Typing for Performance: Apply native types and optimizations precisely where they matter, while the rest of your code remains standard, dynamic PHP.
Typical Use Cases
- High-Performance CLI Tools: Develop standalone command-line applications that benefit from native execution speed and reduced startup times.
- Performance-Critical Web Services: Compile backend services and microservices to achieve near-native performance, handling heavy computational loads with ease.
- PHP Extensions: Create custom, high-performance PHP extensions for specialized tasks, integrating seamlessly with existing PHP environments.
- Reusable Libraries: Package compiled PHP logic into shared libraries that can be consumed by other PHP projects or even external applications.
- Source Code Protection: Distribute compiled native binaries instead of source code, providing a robust layer of intellectual property protection.
- Deterministic Performance: Ensure consistent and predictable execution speed, free from the variability of JIT compilation and interpreter overhead.
