The open-source compiler elephc lets you compile a static subset of PHP into a native executable. The binary runs without a PHP installation or the Zend Engine. Vincenzo Petrucci at Illegal Studio maintains the project.
How It Works
elephc parses your PHP, checks its types, and generates assembly code. It then uses a native assembler and linker to build the executable. It supports macOS ARM64, Linux ARM64, and Linux x86_64.
Supported code compiles ahead of time, with no fallback to PHP opcodes. Experimental eval() support has a separate path: it can compile eligible literal fragments ahead of time, while fragments that need runtime parsing use an optional interpreter linked into the binary.
What It Supports
Some of the supported features include:
- Classes, interfaces, traits, enums,
readonly, attributes, closures, and generators - Namespaces and compile-time Composer autoloading (PSR-4, PSR-0, classmap, and files)
- PDO with SQLite, PostgreSQL, and MySQL/MariaDB
- Image processing, mbstring, XML, curl, and Fibers
- A
--webmode that builds a standalone HTTP server binary, with$_SESSIONsupport - Shared and static libraries, with iOS ARM64 library targets available from a macOS host
Support doesn't mean full PHP compatibility. The README describes the compiler as experimental and warns that you'll find bugs.
A DOOM Level Renderer in PHP
The project's DOOM showcase loads the original game's WAD data files and renders the E1M1 level. You can move around the map, with collision detection and lighting. The demo requires SDL2 and a copy of DOOM1.WAD; it has no sound and uses solid wall colors rather than textures.
The PHP code uses elephc-specific syntax, including extern to call SDL2 through FFI, buffer<T> to store map geometry, and packed class for the WAD data structures.
Can You Use It With Your Framework?
elephc doesn't compile an entire dynamic framework today. The README proposes keeping your WordPress, Laravel, or Symfony application on PHP and compiling static, performance-sensitive modules into native libraries. The CLI already produces libraries, but the PHP extension bridge is still on the roadmap.
For another PHP compiler project, see our coverage of TypePHP.
Getting Started
You can install elephc through its version manager, elvm. Start with:
curl -fsSL https://get.elephc.dev | sh
Accept the installer's prompt to add ~/.elvm/bin to your PATH, then start a new shell and install the compiler:
elvm install latest
elvm use latest --global
On macOS you can also use Homebrew:
brew install illegalstudio/tap/elephc
Then compile and run a file:
elephc hello.php ./hello
You'll need a native assembler and linker: Xcode Command Line Tools on macOS, or as, ld, and libc development files on Linux. Rust is only required if you build the compiler from source.
elephc is MIT licensed. Visit the GitHub repo for the source and installation docs. Its commented code generation and runtime code also let you follow how PHP code turns into CPU instructions.