百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
E

elephc

> 编程语言
开源

一个 PHP 到原生编译器。它采用 PHP 的一部分(包括 eval()),直接编译为原生汇编代码,生成适用于 macOS ARM64 的独立二进制文件。

538 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一个 PHP 到原生编译器。它采用 PHP 的一部分(包括 eval()),直接编译为原生汇编代码,生成适用于 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


Support the project

elephc is built and maintained independently. You can support the project by either:

  • Following me on X (@nahime0) for updates, new features, and behind-the-scenes development.
  • ⭐ Starring the repo — it helps others discover it and keeps the project going.
  • Sponsoring on GitHub — every contribution, big or small, makes a real difference.

Core Contributors

 Vincenzo Petrucci

 Guillaume Loulier

An async HTTP server in PHP

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.

DOOM rendered in PHP

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.

Why

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.

What you can expect

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:

bash
elephc myfile.php
./myfile

elephc 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";
bash
elephc hello.lfc
./hello

LFC 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.

Learn how a compiler works

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.

How elephc is different

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:

  • Less long-tail compatibility than a VM-backed PHP implementation.
  • More mechanical transparency: readable assembly output, source maps, line-by-line commented codegen, and a documented memory model.
  • No hidden external PHP runtime dependency: the generated binary does not need PHP, the Zend Engine, or a loader extension. A program that needs dynamic eval() embeds its optional interpreter bridge directly in the standalone binary.
  • Native-oriented extensions: 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.

Requirements

  • Rust toolchain (cargo) only when building elephc from source
  • A native assembler and linker for your host/target
  • On macOS: Xcode Command Line Tools (xcode-select --install)
  • On Linux: a standard native toolchain (as, ld, libc development files)
  • To install curated native packages: a POSIX shell, Make, target cc, ar, and ranlib

Native packages such as PCRE2 are built from Elephc's pinned catalog; a system PCRE2 package is not a production prerequisite.

Install

elvm (recommended)

elvm installs elephc versions side by side and selects the right compiler through a project or global version. Install the version manager first:

bash
curl -fsSL https://get.elephc.dev | sh

The 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:

bash
elvm install latest
elvm use latest --global
elvm curre

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustassemblycompilerlinuxmacos

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言