Z-Engine reaches straight into the heart of the PHP runtime โ the Zend Engine โ and hands you its internals as ordinary PHP objects. Overload operators, make classes immutable, register real engine modules, rewrite the AST, redefine methods at runtime. No C, no compiler, no recompiling PHP. Just FFI and a lot of nerve.
โ ๏ธ Experimental โ not for production. Z-Engine operates on raw engine memory. Segfaults are a feature of the territory, not a bug in your code.
Ordinary PHP lets you write programs on top of the language. Z-Engine lets you change the runtime those programs run on, from a Composer package and in plain PHP.
You hit this limit sooner or later when you write a framework, a test tool, a profiler or a long-running worker. You need behaviour that only a C extension can give you, so you are left with four choices: write and maintain C, give up the feature, depend on a PECL extension your users cannot install, or fake it with code generation, proxies and stream-wrapper tricks. Z-Engine adds a fifth: do what the C extension would do, from PHP, at runtime, on code that is already loaded. The projects below already do exactly that.
It is a tool for people who build tools: framework and library authors, testing and tooling authors, people who run long-lived workers (RoadRunner, Swoole, FrankenPHP, ReactPHP, AMPHP), and anyone prototyping a language idea before writing an RFC. It is not meant for the business logic of a typical application.
Every project below needed something PHP does not offer from userland. The usual answer would have been a C extension, a patched PHP build, or no feature at all. Each one shipped a composer require instead.
| Project | What it needed | What it would have needed without Z-Engine |
|---|---|---|
| Swoole Bundle (Symfony on Swoole) | Per-coroutine proxies for stateful services, including final classes from vendor libraries |
uopz or runkit7 on every server, or forking each vendor class |
| Moxio SQLite Extended API | Load the SpatiaLite extension into a PDO SQLite connection behind Doctrine DBAL | A patched pdo_sqlite. PHP only gained Pdo\Sqlite::loadExtension() in 8.4, four years later |
| rikudou/units | $meter + $feet arithmetic on unit objects |
An operator-overloading extension such as pecl/operator |
| phpinnacle/mure | C#-style extension methods on classes you do not own, such as DateTime |
runkit7 |
| ZDebug | Step debugging from PhpStorm or VS Code | Xdebug |
| Scalar Objects | Methods on strings, ints and arrays | nikic/scalar_objects, compiled for each PHP version |
| Shared Data Extension | Objects that survive across requests, with no serialization | APCu, which serializes and copies on every fetch |
With coroutines, one worker serves many requests at the same time, so every stateful service needs a proxy that keeps separate state per coroutine. A proxy has to extend the class, and many vendor classes are final. The bundle's docs say they found no better solution than removing the flag at runtime:
// swoole-bundle, src/Reflection/ClassModifier.php (shortened)
$reflClass = new \ZEngine\Reflection\ReflectionClass($className);
$reflClass->setFinal(false);
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_FINAL) as $reflMethod) {
$reflMethod->setFinal(false);
}
// the container can now generate a per-coroutine proxy that extends $classNameMoxio ran integration tests on SQLite with the SpatiaLite geospatial extension and wanted to move to Doctrine DBAL, which is built on PDO. PDO had no way to load an SQLite extension, and the php-src pull request that would have added it had not been merged. Z-Engine let them resolve the PDO object to the engine's own zend_object, follow it to the native sqlite3* connection, and call the SQLite C API through FFI (the full story):
use Moxio\SQLiteExtendedAPI\Facade;
$pdo = new \PDO('sqlite::memory:');
Facade::wrapPDO($pdo)->loadExtension('mod_spatialite.so'); // sqlite3_load_extension() on PDO's own handle// rikudou/units: operators through the engine's do_operation handler
$result = new Meter(5) + new Foot(15); // Meter, value 9.572 (BigNumber precision)
// phpinnacle/mure: C#-style extension methods
class MyDate {
public static function addDays(\DateTime $date, int $num) { return $date->modify("+$num days"); }
}
\PHPinnacle\Mure\ExtensionManager::init(MyDate::class);
echo (new \DateTime())->addDays(2)->format('d.m.Y');# ZDebug: your IDE attaches over DBGp, Xdebug's own protocol; nothing is compiled
composer require --dev lisachenko/zdebug:dev-main
php -d ffi.enable=1 -d opcache.jit=off -d auto_prepend_file=vendor/lisachenko/zdebug/bootstrap/zdebug.php app.php// Scalar Objects: a compile-time AST rewrite, in place of the nikic/scalar_objects extension
" hello "->trim()->upper(); // "HELLO"
[1, 2, 3, 4]->filter(fn ($x) => $x % 2 === 0)->sum(); // 6
// Shared Data: build once per worker, get the same live object back in every later request
$config = $store->has(AppConfig::class)
? $store->get(AppConfig::class)
: $store->persist(AppConfig::class, buildExpensiveConfig());Users also ask about: intercepting every call through the engine's observer API (#51), hooking === (#39), dead-code detection (#35), custom keywords (#17) and redefining functions REPL-style (#15, now redefine()).
Beyond the projects above, these are things the library supports today. Each case starts with the conventional PHP answer and its limit, then shows the Z-Engine way.
Usually: a worker that keeps the framework booted between requests must be restarted to pick up a code change, which throws away warm caches, open connections and in-memory state. Dev servers do it on every file save.
With Z-Engine: swap the new class definition into the running process. Live objects run the new method bodies straight away. The swap is atomic: all of it applies or none of it does.
use ZEngine\HotSwap\HotSwap;
// Called by your file watcher when src/Pricing/PriceCalculator.php changes
$source = preg_replace('/^<\?php\s*/', '', file_get_contents($changedFile));
$delta = HotSwap::prepare(PriceCalculator::class, $source);
$delta->getChangedMethods(); // ['calculate', ...]: log or confirm before applying
$delta->apply(); // every existing PriceCalculator now runs the new codeChanged or added methods, constants and default values are supported. Changes that would break live instances (a different property layout or class hierarchy) are refused with a HotSwapException before anything is touched. โ docs/hot-swap.md
Usually: PHPUnit cannot mock a final class. Workarounds such as dg/bypass-finals rewrite the source in a stream wrapper, which only works for classes not loaded yet and conflicts with opcache. uopz works, but it is a C extension you have to install on every CI image.
With Z-Engine: change the loaded class itself.
use ZEngine\Reflection\ReflectionClass;
use ZEngine\Reflection\ReflectionMethod;
// A final SDK client from vendor/ that would otherwise hit a real payment API
(new ReflectionClass(PaymentClient::class))->setFinal(false); // createMock() works now
// ...or patch one method, keeping the original callable
(new ReflectionMethod(PaymentClient::class, 'charge'))->redefine(
fn (Money $amount): Receipt => Receipt::fake($amount),
preserveAs: 'chargeForReal',
);Usually: to time or log calls on a class you do not own, you generate a decorator or proxy class, wire it into the container, and hope nobody calls new directly. Or you install a C-level APM agent.
With Z-Engine: wrap the method body in place. Every caller is covered, new and static calls included, because the engine's own function entry is changed.
(new ReflectionMethod(OrderRepository::class, 'find'))->redefine(
function (int $id): ?Order {
$start = hrtime(true);
try {
return $this->findUntraced($id); // the original body
} finally {
Tracer::record('OrderRepository::find', hrtime(true) - $start);
}
},
preserveAs: 'findUntraced',
);For compile-time rewriting, a zend_ast_process hook (Core::setASTProcessHandler()) lets you change the AST of every file as it is compiled. You can also patch opcache's file-cache binaries so the cached code is transformed before it runs (โ docs/opcache-binary.md).
Usually: money, decimals, vectors and matrices end up as $total->add($tax)->multiply($qty), and == compares properties instead of values.
With Z-Engine: give the class real operators through the engine's do_operation and compare handlers, the same mechanism GMP and BcMath\Number use in C.
$total = $price * $quantity + $shipping; // Money objects, not floats
if ($total > $creditLimit) { /* ... */ } // compared by amountThe full example is under Operator overloading. native-php-matrix is a complete Matrix type built this way.
Usually: Collection<User> exists only in docblocks. PHPStan and Psalm check it, but at runtime anything can be pushed in.
With Z-Engine: turn a template class into a real, separate class whose type declarations are rewritten, so the engine's own type checks enforce them.
$users = (new ReflectionClass(TypedList::class))->specialize(
'App\TypedList_User',
new TypeSubstitutionMap(['App\T' => User::class]),
)->newInstance();
$users->add(new User()); // fine
$users->add('bob'); // TypeError, raised by the engineuserland-php-generics builds Box::of('int')-style generics on top of this. โ docs/class-specialization.md
Usually: trying out new syntax or semantics means patching php-src in C, rebuilding PHP, and asking reviewers to do the same.
With Z-Engine: rewrite the AST at compile time, install your own handler for any VM opcode, add functions to the engine's function table, or register a full engine module with persistent globals. A prototype becomes a composer require that anyone can try.
- No C, no compiler, no PECL.
composer requireworks anywhere ext-ffi is enabled: Linux, macOS and Windows, NTS and ZTS. - Works on code that is already loaded, including vendor classes and opcache-cached code. Source-rewriting tools only see a class before it is loaded; Z-Engine changes the live engine structures, so preloaded and cached code is in scope too.
- Real engine semantics, not emulation. A specialized class is a real class entry, a substituted type is checked by the engine, and a generated function (
ReflectionFunction::addFunction()) runs through the normal VM with no FFI overhead per call. - Atomic, reversible changes. Hot swaps commit completely or roll back byte for byte. Hooks have
install()/uninstall(), and all of them are restored at shutdown. - Byte-exact by construction. Engine definitions are generated from the real PHP build for each version, OS, architecture and thread-safety mode.
Core::init()refuses to run on a PHP minor it was not built for, and can check every struct layout before it touches memory. - Built for long-running processes. A clear ownership model with deterministic releases (see Memory safety) keeps worker loops and FPM with preload stable.
- Typed, analysable API. Consumers get PHP objects (
ReflectionClass,ReflectionValue,PersistentHeap, โฆ), not raw pointers. The codebase passes PHPStan at level max, and generated stubs type every engine struct.
- When PHP already has the feature. Use
readonly, property hooks, asymmetric visibility, enums or first-class callables where they are enough. - In production request paths, for now. The project is experimental, and a wrong move crashes the process instead of throwing an exception.
- When the JIT is required. The JIT rewrites the executor internals Z-Engine hooks into, so it must be off (
opcache.jit=off). - When FFI is disabled. Hosts that set
ffi.enable=0, and cannot setffi.enable=preloadtogether with opcache preload, cannot run it.
FFI lets PHP load shared libraries, call C functions, and read C structures without a compiler or a third intermediate language. Z-Engine points that power back at PHP itself. It ships generated, version-exact FFI definitions of the engine's structures for each supported PHP version, and a runtime that refuses to boot unless the definitions match your interpreter down to the byte. That byte-exactness is what turns "insanely dangerous" into "dangerous but disciplined."
- PHP with the FFI extension enabled
Engine memory layouts change between every PHP minor version, so each PHP minor has its own generated definitions and its own branch.
| PHP | OS / Arch / TS | Branch | Status |
|---|---|---|---|
| 8.6 | linux-x64 (nts, zts), darwin-x64 (nts, zts), darwin-arm64 (nts, zts), windows-x64 (nts, zts) | master |
โ supportedยน |
| 8.5 | linux-x64 (nts, zts), darwin-x64 (nts, zts), darwin-arm64 (nts, zts), windows-x64 (nts, zts) | 8.5 |
โ supported |
| 8.4 | linux-x64 (nts, zts), darwin-x64 (nts, zts), darwin-arm64 (nts, zts), windows-x64 (nts, zts) | 8.4 |
โ supported |
| 8.0 | linux-x64-nts | 8.0 |
๐ง frozen (legacy) |
ยน Supported since the PHP 8.6.0 release: the committed linux definitions regenerate byte for byte from the 8.6.0 GA build, and CI re-verifies the darwin ones on macOS runners. The windows-x64 definitions come from the "Generate windows headers" workflow, run against the windows.php.net 8.6 builds.
Version matching is not optional. Running Z-Engine against a PHP minor it was not built for corrupts memory.
Core::init()enforces the match and aborts with a clear message rather than letting you crash.
composer require lisachenko/z-engineThere is nothing to initialize: the engine bridge is booted from Composer's autoloader, so
require __DIR__ . '/vendor/autoload.php' is all a consumer needs. That includes the
opcache.preload stage, which the bootstrap recognises and serves by publishing the engine
definitions for the life of the server rather than for the preload request alone โ pointing
opcache.preload at a script that only requires the autoloader is enough to get the
per-request cost down.
opcache.preload does not exist on Windows, so there the definitions cannot be published once
at server start: every process binds them for itself. That needs no change on your side either โ
it is the same autoload bootstrap, taking the other branch.
PHP's default ffi.enable=preload allows the FFI API on the CLI, but at request time in FPM,
Apache or the built-in server only from code that opcache.preload compiled at server start.
So a web SAPI needs z-engine to be part of the preload stage โ nothing else can bind the
engine there, not even a header published through ffi.preload, because the restriction is on
the code that calls FFI, not on the definitions. The whole configuration is (php.ini or an FPM
php_admin_value, never .user.ini or ini_set(); restart the server afterwards):
opcache.enable=1
opcache.preload=/path/to/vendor/lisachenko/z-engine/preload.php
opcache.preload_user=www-data ; FPM only, when the master process runs as rootAny preload script that requires your Composer autoloader does the same job as the bundled one.
When the bridge cannot bind under preload mode, Core::init() throws a RuntimeException
whose message is this recipe with the paths resolved for the failing build, plus a diagnosis
of the settings already in place; Core::preloadConfigurationHint() returns the same text on
demand. Two more directives are worth knowing:
-
ffi.preloadpublishes the C definitions once at module startup, so no request parses the header. Its value is the generated header for the exact build โ minor, OS, architecture and thread-safety mode โ whichCore::engineHeaderPath()resolves; run it under the PHP the server uses, since a CLI binary can be a different build than php-fpm:php -r 'require "vendor/autoload.php"; echo ZEngine\Core::engineHeaderPath();'It is optional next to
opcache.preload(z-engine takes a scope that is already published instead of loading the header again), and it is the way to bind once underffi.enable=1. -
ffi.enable=1allows FFI for every caller; z-engine then binds the definitions per request, or once at startup withffi.preload. On Windows, whereopcache.preloaddoes not exist, this is the only way to run a web SAPI.
Core::init() remains public, idempotent and re-invocable, for the cases that want it: booting
explicitly at a chosen point, re-booting after Core::shutdown() inside a live worker, and
turning "the engine is not available here" into its explanation. On a host that cannot run the
engine at all โ no ext-ffi, ffi.enable=0, an unsupported PHP minor or platform โ autoloading
stays silent and leaves Core uninitialized, so static analysis and test suites still load the
package; ask Core::isInitialized() for the state, or call Core::init() to get the reason.
Set ZENGINE_AUTOBOOT=0 to skip the automatic boot entirely.
<?php
declare(strict_types=1);
use ZEngine\Reflection\ReflectionClass;
require __DIR__ . '/vendor/autoload.php';
final class Sealed {}
$reflection = new ReflectionClass(Sealed::class);
$reflection->setFinal(false);
eval('class Extended extends Sealed {}'); // ...it just works.Using PHP FFI, Z-Engine loads the exact C struct definitions of the running engine (zend_class_entry, zval, zend_object_handlers, zend_module_entry) and changes them the way a compiled C extension would:
| Capability | What you can do |
|---|---|
| ๐ช Reflection on steroids | Make a final class non-final, add interfaces and methods at runtime, redefine method bodies, hot-swap whole classes |
| ๐งฎ Operator overloading | Give your objects real +, -, *, /, **, == semantics via the engine's do_operation and compare handlers |
| ๐ Custom object handlers | Hook create_object, read/write/unset_property, cast_object, get_property_ptr_ptr to build truly immutable objects, copy-on-write types and proxies |
| ๐งฌ Class specialization | Clone a class under a new name with its types rewritten (the basis for reified generics) |
| ๐งฉ Runtime engine modules | Register a genuine zend_module_entry at runtime, with persistent globals shared across requests: an extension written entirely in PHP |
| ๐พ Persistent heap | Keep whole object graphs alive across requests in a worker process |
| ๐ณ Abstract Syntax Tree access | Parse source to the engine's own AST, inspect it, and rewrite it through the zend_ast_process hook |
| โ๏ธ Opcode handlers | Install your own handler for any VM opcode |
ZEngine\Reflection\ReflectionClass and ReflectionMethod extend the native reflection classes with write access to the engine:
$class = new ReflectionClass(Sealed::class);
$class->setFinal(false); // un-final a class
$class->setAbstract(true); // make it abstract
$class->addInterfaces(Countable::class); // graft on an interface at runtime
$class->addMethod('count', fn() => 42); // add a method from a closure
$method = new ReflectionMethod(Service::class, 'handle');
$method->setPublic();
$method->redefine(fn() => 'patched'); // swap the method body
$method->redefine(fn() => 'wrapped:' . $this->handleOriginal(), preserveAs: 'handleOriginal'); // ...keeping the previous body callable
$method->redefine($donor->getMethod('handle')); // or adopt the body of another class's methodTurn a closure into a genuine engine function or method. Unlike a closure installed into an engine handler field (which ext/ffi calls back into through a slow libffi trampoline), a generated function is published straight into the engine's function table and afterwards dispatches through the normal Zend VM with zero FFI at call time โ exactly as fast as any ordinary PHP function:
use ZEngine\Reflection\ReflectionFunction;
ReflectionFunction::addFunction('twice', fn (int $x): int => $x * 2);
twice(21); // 42 โ a real global function, no trampoline
$class->addMethod('scale', fn (float $k) => ...); // same, as a methodSee docs/memory-model.md for how PHP zvals, FFI trampolines and native C handlers map to memory, and why this path is fast.
Give your value objects native arithmetic. Implement the extension interfaces and install the handlers with one call:
use ZEngine\ClassExtension\ObjectCreateInterface;
use ZEngine\ClassExtension\ObjectCreateTrait;
use ZEngine\ClassExtension\ObjectDoOperationInterface;
use ZEngine\ClassExtension\ObjectCompareValuesInterface;
use ZEngine\ClassExtension\Hook\DoOperationHook;
use ZEngine\Reflection\ReflectionClass;
class Matrix implements ObjectCreateInterface, ObjectDoOperationInterface, ObjectCompareValuesInterface
{
use ObjectCreateTrait;
public static function __doOperation(DoOperationHook $hook): self { /* ... */ }
// public static function __compare(CompareValuesHook $hook): int { ... }
}
(new ReflectionClass(Matrix::class))->installExtensionHandlers();
$c = new Matrix([10, 20, 30]) + new Matrix([1, 2, 3]); // Matrix([11, 22, 33])
$c *= 2; // โ Matrix([22, 44, 66])No access to the class source (e.g. it lives in vendor/)? Install the handlers imperatively instead:
$class = new ReflectionClass(Matrix::class);
$class->setCreateObjectHandler(Closure::fromCallable([ObjectCreateTrait::class, '__init']));
$class->setWritePropertyHandler(fn ($hook) => /* ... */);The available object hooks are create_object, cast_object, do_operation, compare, read_property, write_property, has_property, unset_property, get_property_ptr_ptr, get_properties_for, and interface_gets_implemented.
Install the
create_objecthandler first โ the other hooks live in memory that it allocates. Internal classes can't receive acreate_objecthandler.
Look up any live object by its handle โ an API PHP itself doesn't expose:
$instance = new stdClass();
$entry = Core::$executor->objectStore[spl_object_id($instance)];Parse PHP source to the engine's own AST and walk it:
$ast = Core::$compiler->parseString('echo 2 + 2;');
echo $ast->dump();You can also install a zend_ast_process hook to rewrite the AST of every file as it compiles.
Read the binary files opcache writes for opcache.file_cache, patch the compiled script through the framework wrappers, and write a valid binary back โ so the engine loads and executes your patched code on the next request:
use ZEngine\OpCache\BinaryCacheFile;
$file = BinaryCacheFile::compile(__DIR__ . '/Service.php', $cacheDir);
$reflection = $file->getReflection(); // ReflectionExtension-shaped handle over the cached script
// ... mutate literals, opcodes, flags through the usual wrappers ...
$file->refresh(); // rewrite the binary + invalidate the sourceThe payload is re-serialized from the mutated graph (not just byte-poked), so size-changing edits are written correctly. See docs/opcache-binary.md for the format, build-matching rules and current limits โ this is the foundation for AOP, transpiling and source-code protection on top of the file cache.
Statement-level interception with named live frame variables โ the raw material for a pure-PHP step debugger:
use ZEngine\System\Compiler;
use ZEngine\System\ExecutionData;
use ZEngine\System\OpCode;
Core::$compiler->setOptions(Core::$compiler->getOptions() | Compiler::COMPILE_EXTENDED_STMT);
OpCode::setHandler(OpCode::EXT_STMT, function (ExecutionData $frame): int {
$line = $frame->getOpline()->getLine();
$locals = $frame->getLocalVariables(); // ['n' => ReflectionValue, ...]
return Core::ZEND_USER_OPCODE_DISPATCH;
});Engine error and VM interrupt hooks (Core::setErrorCallbackHandler(),
Core::setInterruptHandler() + Core::$executor->requestInterrupt()) round out the
breakpoint/pause primitives. See docs/self-debugging.md
for the full feasibility research: what an Xdebug-equivalent can and cannot do from
pure PHP, and why โ and ZDebug for the
debugger built on it: a DBGp step debugger your IDE attaches to, with no C extension.
Register a real engine module at runtime, complete with persistent globals shared across requests:
use ZEngine\EngineExtension\AbstractModule;
final class Counter extends AbstractModule
{
protected static function globalType(): ?string { return 'unsigned int[10]'; }
}
$module = new Counter('counter');
$module->register();
$module->startup();
$globals = $module->getGlobals(); // FFI-backed, survives across requestsEvery value wrapper follows an explicit ownership model: owning constructors take their own
engine reference and release it deterministically (release()/destruction), fromCData()
factories stay borrowed, and all releases go through the engine's own primitives
(zval_ptr_dtor/rc_dtor_func) โ never through the FFI allocator. Engine hooks have a full
lifecycle (install()/uninstall()/reinstall()) backed by a registry, and Core::shutdown()
(registered automatically) restores every hooked engine pointer before the engine could ever
call a freed trampoline โ which is what makes worker loops and FPM + opcache preload viable.
Notable behaviour changes compared to older releases:
new StringEntry()/new ObjectEntry()/new ResourceEntry()addref and keep their target alive for the wrapper lifetime;ClosureEntry::setThis()releases the old bound$thisand references the new one (no more "object must outlive the closure").Compiler::parseString()trees free themselves when the last node wrapper is collected.AbstractHook::__destruct()no longer force-restores pointers at arbitrary GC moments.
See docs/long-running.md for the ownership tables, the hook lifecycle, runtime models (worker vs FPM), and the short list of immortal-by-design allocations.
- lisachenko/native-php-coroutines: Fiber coroutines plus forked workers that share real PHP objects
- lisachenko/immutable-object: mark a class immutable with a single interface; property writes outside the constructor throw
Z-Engine has a couple of unusual rules โ most importantly, match your PHP version to the branch and develop against a debug build. See CONTRIBUTING.md and AGENTS.md (the full contract for humans and automated tools). Engine definitions are generated from the PHP source by tools/generator/ and never hand-edited.
composer test # safe suite
composer test:internal # destructive tests, on a debug PHP build
composer phpstan # static analysis at level max
composer cs:check # coding standardsReleased under the MIT License.