Developers can compile PHP into native machine code without an interpreter
TypePHP is an AOT (Ahead-Of-Time) tool that converts PHP source code into C++ and compiles it into a native binary, PHP extension, or shared library. This process allows PHP scripts to run directly on the CPU as machine code without a separate interpreter. It maintains existing PHP syntax while using type information at compile time to generate optimized code.
Native execution without an interpreter
TypePHP does not convert PHP code to bytecode for execution; instead, it lowers it to C++17 code and compiles it into native machine code. During this process, primitive types like int, float, and bool are mapped directly to C++ scalar types, allowing numeric operations to be converted into CPU instructions. The compiler itself is written in PHP, and the tpc binary is generated by compiling the TypePHP compiler's PHP source, creating a self-hosting structure. Because there is no opcode interpretation or JIT warm-up at runtime, startup time is fast and performance is deterministic.
Three build modes and precise numeric support
The same codebase can be built in three forms: a native executable (bin), a PHP extension (ext), and a shared library (lib).
- Native executable Creates a binary that runs independently without PHP CLI. Since the source code is converted to machine code, this provides source protection during distribution.
- PHP extension and library Allows compiled functions or classes to be loaded as extensions in existing PHP projects, or built as shared libraries for reuse in other projects.
- Precise numeric types For integers with 54 or more digits or operations requiring no floating-point error, use
bigInt,decimal, andbigFloatto obtain accurate results.
Compatibility scope and limitations
TypePHP intentionally supports a testable subset of PHP rather than all dynamic PHP programs. The global scope allows only declarations, and executable statements must be inside functions or methods. In binary mode, the signature of the main() function is strictly defined. Inferred int, float, and bool variables use fixed native storage by default, so they cannot be changed to incompatible types later. Some dynamic references, closures, and reflection patterns are intentionally unsupported, so you must verify the compatibility model and list of incompatible features before introducing it into existing applications.
Environment and conditions to check before adoption
Using TypePHP requires PHP 8.4 or 8.5 CLI, development headers, and php-config. On Unix-like systems, the embedded library for the corresponding PHP version (libphp.so or libphp.dylib) is required for building binaries or shared libraries. You must have GCC 9 or later or Clang, CMake 3.24 or later, and Composer 2 installed, along with GMP and MPFR libraries for precise numeric operations. Linux x64 is the primary development and testing platform, but Windows, macOS, ARM64, Android, iOS, and WASI backends are also supported. For mobile app development, it is possible to write UI structure and business logic in TypePHP while maintaining a thin native UI bridge. Using the --nano option allows compiling PHP Nano and PHPX source directly without the Zend opcode interpreter to create lighter executables, but in this case, VM entry paths like eval and include, as well as anonymous classes, cannot be used.