Loading repository data…
Loading repository data…
illegalstudio / repository
A PHP-to-native compiler. Takes a subset of PHP - including eval() - and compiles it directly to native assembly, producing standalone binaries for macOS ARM64, Linux ARM64, and Linux x86_64. No VM, no external runtime, no dependencies: dynamic eval falls back to an optional statically linked interpreter.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
elephc is built and maintained independently. You can support the project by either:
An asynchronous HTTP/1.1 server — a non-blocking poll() event loop, one Fiber per connection, raw TCP sockets through extern FFI, plus an HTTP parser and a router — written entirely in PHP and compiled to a single native binary. No interpreter, no PHP-FPM, no Nginx.
See showcases/http-server/ for full source and build instructions.
The flagship showcase: a real-time 3D renderer that loads original DOOM WAD files and renders E1M1 — BSP traversal, perspective projection, per-column fog, sector lighting, collision detection, step climbing — entirely in PHP compiled to a native binary.
See showcases/doom/ for full source and build instructions.
My first "serious programming" book was PHP 4 and MySQL. After years of experimenting with code, that book turned my passion into a profession. I've worked with many languages over the past 20 years, but PHP is the one that has most consistently put food on the table.
PHP has a simple, approachable, and elegant syntax. Millions of developers worldwide already know it well. That makes it an ideal bridge to bring web developers closer to lower-level programming — systems work, native binaries, understanding what happens under the hood — without forcing them to learn an entirely new language first.
One thing I always missed about PHP was the ability to produce optimized, fast native binaries. While everyone else is busy building the next Facebook, I thought I could try to fill that gap and write a compiler for PHP.
Of course, PHP has its limits when it comes to performance-critical or systems-level work. That's why elephc introduces compiler extensions like packed class for flat POD records, for contiguous typed arrays, for raw memory access, and for FFI — constructs that give PHP developers the tools they need without abandoning the language they already know.
buffer<T>ptrexternIt's not perfect, but it works — and it has grown into a genuinely capable PHP compiler. It also happens to be a great way to understand how a compiler works and how assembly language operates under the hood.
I made the project as modular as possible. Every function has its own codegen file, and each one is commented line by line, so you can see exactly how a high-level construct gets translated into its low-level equivalent.
You can write PHP using the constructs documented in the docs. Classes with single inheritance, interfaces, instanceof, nullsafe access (?->), abstract classes, final classes, methods and typed/static properties, PHP-style static property redeclarations, constructor property promotion, traits, constructors, instance/static methods, case-insensitive PHP symbol lookup for functions/classes/methods, self:: / parent:: / static:: with late static binding, readonly properties and classes, enums, PHP 8 attributes on declarations, named arguments, first-class callables, typed function and method parameters and returns, try / catch / finally / throw, visibility modifiers, union and nullable types, copy-on-write arrays, associative arrays with PHP insertion order and integer/numeric-string key normalization, array union with +, closures, generator functions and generator closures with yield / yield from, namespaces, includes, compile-time Composer/SPL autoloading, class/introspection helpers, PDO database access (PDO / PDOStatement / PDOException) with SQLite, PostgreSQL, and MySQL/MariaDB drivers, image creation and manipulation (GD raster I/O, drawing, transforms/filters, Exif/IPTC metadata, and the Imagick/Gmagick/Cairo object APIs) on a pure-Rust codec/raster bridge, and PHP 8.1-style Fiber coroutines on macOS ARM64, Linux ARM64, and Linux x86_64.
Experimental eval() support AOT-lowers eligible literal fragments and falls back to the optional, statically linked Magician interpreter for dynamic fragments. Runnable examples live in examples/eval/ and examples/eval-globals/.
For performance-oriented code, elephc exposes compiler extensions beyond standard PHP — see the Why section above.
Then compile and run:
elephc myfile.php
./myfile
The compiler is experimental and evolving. Not everything PHP supports is implemented, and you will find bugs. But as the DOOM showcase demonstrates, you can build real, non-trivial programs with it today.
If you want to contribute, you're welcome. Mi casa es tu casa.
elephc is designed to be read. The code generation and runtime layers are heavily annotated, so you can see what each lowering step and emitted instruction is doing — from stack frame setup to syscall invocation, from integer-to-string conversion to array memory layout. If you've ever wondered what happens between echo "hello" and the CPU executing it, follow the code from src/codegen/ and read the comments. No prior assembly knowledge required.
There are several ways to make PHP easier to distribute or faster to run: bundling a PHP runtime into one executable, encrypting bytecode, running through the Zend VM with JIT, or compiling selected hot paths while falling back to opcodes for dynamic code.
elephc takes a narrower but cleaner route: it is a from-scratch compiler for a static subset of PHP. It parses PHP source, type-checks it, lowers it to target-specific assembly, assembles and links it into a native executable, and ships only the small runtime routines needed by the generated program. Ordinary supported constructs are native code. Eligible literal eval() fragments can also be lowered ahead of time; fragments that require runtime parsing use the optional Magician interpreter bridge.
That tradeoff is intentional:
eval() embeds its optional interpreter bridge directly in the standalone binary.extern, ptr, buffer<T>, and packed class let PHP-shaped code cross into systems, FFI, game, and performance-sensitive workloads.That does not mean elephc has to live outside the existing PHP ecosystem. The current CLI path produces standalone executables, but the roadmap also includes shared/static library output and an experimental PHP extension bridge. That opens a practical middle path: keep a framework such as WordPress, Laravel, or Symfony running on PHP, then compile static, performance-sensitive modules into native libraries or PHP extensions.
So elephc is not a drop-in replacement for an entire dynamic framework today. The longer-term goal is more useful: make it possible to move the parts of PHP code that are static enough to compile into inspectable native code, while the rest of the application can stay in ordinary PHP.
cargo)xcode-select --install)as, ld, libc development files)brew install illegalstudio/tap/elephc
git clone https://github.com/illegalstudio/elephc.git
cd elephc
cargo build --release
The binary is at ./target/release/elephc.
Pre-built binaries are available on the Releases page. If macOS blocks the binary, run:
xattr -cr elephc
Important: elephc lowers every build through the EIR pipeline and the target-aware assembly emitter.
# Compile a PHP file to a native binary
elephc hello.php
./hello
# Custom heap size (default: 8MB)
elephc --heap-size=16777216 heavy.php
# Enable runtime heap verification while debugging ownership issues
elephc --heap-debug heavy.php
# Print allocation/free counters to stderr while debugging GC behavior
elephc --gc-stats heavy.php
# Enable compile-time feature branches
elephc --define DEBUG app.php
# Print per-phase compiler timings
elephc --timings hello.php
# Emit assembly and a simple source-map sidecar
elephc --emit-asm --source-map hello.php
# Run the front-end checks without writing assembly or a binary
elephc --check hello.php
# Fall back to stack-only value placement (default is linear-scan registers)
elephc --regalloc=stack hot.php
# Disable the EIR optimization passes (identity folding, peepholes, dead instruction elimination, …) for A/B comparison
elephc --no-ir-opt hot.php
# Link extra