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,
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,
Elephc
Pronounced el-ef-see — just spell out “LFC”.
Write PHP. Ship a native binary.
5 compile targets · 3 release hosts · no Zend Engine · no external PHP runtime
A PHP-to-native compiler that takes a subset of PHP and compiles it directly to native assembly. Standalone executables and compiler release archives target macOS ARM64, Linux ARM64, and Linux x86_64; a macOS host also cross-compiles libraries for iOS ARM64 devices and the iOS ARM64 Simulator. Ordinary source is AOT-compiled with no opcode fallback; experimental eval() can embed an optional interpreter bridge when runtime parsing is required.
Official Website
elephc is built and maintained independently. You can support the project by either:
Vincenzo Petrucci
Guillaume Loulier
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, buffer for contiguous typed arrays, ptr for raw memory access, and extern for FFI — constructs that give PHP developers the tools they need without abandoning the language they already know.
It'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, MySQL/MariaDB, and optional DBLIB, Firebird, ODBC, Informix, IBM, SQLSRV, and Oracle 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 the three executable/release hosts: macOS ARM64, Linux ARM64, and Linux x86_64. The iOS compile targets are library-only and do not run Fibers.
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/, examples/eval-globals/, and the opt-in regex example examples/eval_regex/.
For performance-oriented code, elephc exposes compiler extensions beyond standard PHP — see the Why section above.
Then compile and run:
elephc myfile.php
./myfileelephc also accepts tagless .lfc source. The whole file is code, so it has no
`` tags and never emits plain text implicitly:
echo "Hello from LFC!\n";elephc hello.lfc
./helloLFC files always expose elephc extensions. In mixed projects, --strict-php
still audits every physical .php file while included or autoloaded .lfc
files keep their extension-enabled profile. See
LFC source files.
Compiled programs profile at the PHP level, with one command that does not
change with the environment. Build with --with-monitoring and elephc monitor reads the binary you ran, or the service already serving traffic at
https://host:9411.
A program you launch is measured from inside: exact wall time, allocations,
retained objects, database wait, SQL queries, outgoing network operations and
network wait, plus call counts, rooted at {main}, so an N+1 is a certainty
rather than a suspicion. Curl requests also propagate the active W3C
traceparent unless user code supplied one. File I/O is not yet counted or
timed. A service you connect to answers from its sample ring by
default — sampled CPU-time shares, sampled allocation attribution and route
tags, with no blocked wall time or query/wait summary in a combined monitoring
build — and elephc monitor --exact returns the measured per-function table for one completed request,
which is the same kind of answer a laptop run gives. A --web service also
answers a signed X-Elephc-Query header, which measures that one request exactly
and leaves the rest untouched. The capability
is dormant until asked, and asking takes the build key — a control channel for a
program you launch, a mutual handshake for one you connect to, a signed
X-Elephc-Query header for a single production request. A project's performance
budget lives in a .elephc file and fails the build when it is exceeded, and
every profile carries a W3C Trace Context identity, so it joins whatever
distributed trace its caller already belongs to. See
Profiling.
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, 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, shared libraries (--emit cdylib), and static libraries (--emit staticlib; --emit lib is an alias), while the roadmap includes 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) only when building elephc from sourcexcode-select --install)as, ld, libc development files)cc, ar, and ranlibNative packages such as PCRE2 are built from Elephc's pinned catalog; a system PCRE2 package is not a production prerequisite.
elvm installs elephc versions side by
side and selects the right compiler through a project or global version. Install
the version manager first:
curl -fsSL https://get.elephc.dev | shThe installer prompts to add ~/.elvm/bin to your PATH; start a new shell
after accepting. It installs elvm itself, not a compiler version. Install the
latest elephc release and make it the default with:
elvm install latest
elvm use latest --global
elvm curreNo open issues yet, or sync has not completed.