php-bindings
Skill855 starsChanged 10 days agoApache-2.0View on GitHub
Use when ext-php-rs needs a Zend symbol it doesn't bind yet, when editing allowed_bindings.rs, src/wrapper.c/.h or src/ffi.rs, when regenerating docsrs_bindings.rs (tools/update_bindings.sh), when code must differ between PHP versions, ZTS/NTS or debug builds (php81…php86, php_zts, php_debug cfgs), when adding support for a new PHP version or API number (crates/php-build ApiVersion), or for build.rs, Windows (windows_build.rs, vectorcall, devel packs) and static-linking build problems, or an `undefined symbol` from `cargo php stubs`.
In this repository6 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- pr-descriptionSkill
- proc-macrosSkill
- semver-breaking-changesSkill
- zend-memorySkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 55
Never declare a Zend function in a private extern "C" block or hard-code a struct layout to avoid the allowlist: the layout differs across …
- Very specific: line 102
Run the version's build in a matching environment (nix .#php86, docker, or CI).
- Very specific: line 111
Read the php-src code of a status getter before you trust it.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Bindings, PHP versions and the build script
How the bindings are made
build.rsfinds PHP ($PHP/php,$PHP_CONFIG), reads its API number and ZTS/debug flags throughcrates/php-build(PHPInfo,ApiVersion), then runs bindgen onsrc/wrapper.hwith an allowlist: only the names inallowed_bindings.rsare generated. Extension authors can add their own throughEXT_PHP_RS_ALLOWED_BINDINGS=name1,name2.unix_build.rs/windows_build.rssupply the include paths, defines and link arguments per platform. On Windows,windows_build.rsdownloads the PHP devel pack for the detected version, TS/NTS and architecture. This repository links withrust-lldthrough.cargo/config.toml;windows_build.rsonly warns when an extension builds with the MSVC linker.src/wrapper.cbuilds into a staticwrapperlibrary and holds C helpers for what bindgen can't bind: Zend macros andstatic inlinefunctions (ext_php_rs_zend_string_init,ext_php_rs_zend_object_release, the globals accessors,ext_php_rs_zend_try_catch, …). Their Rust declarations are written by hand insrc/ffi.rs(bindgen would put them under#[link(name = "php")]).- docs.rs has no PHP: with
DOCS_RSset,build.rsuses the checked-indocsrs_bindings.rsand sets fixed cfgs (php_debug,php81…php85; nophp86, nophp_zts), so code behind other cfgs is not documented there.
Adding a Zend symbol
- A function, type, constant or global that
php.h(or a headerwrapper.hincludes) declares: add its name toallowed_bindings.rs, in the list's order style. A header that isn't included yet goes intosrc/wrapper.h. - A macro or
static inlinefunction: add anext_php_rs_*helper tosrc/wrapper.c, its prototype tosrc/wrapper.h, and the declaration to the#[link(name = "wrapper")]block insrc/ffi.rs. Keep the helper a thin wrapper around the macro, so the Zend semantics stay in Zend. - Regenerate
docsrs_bindings.rswithtools/update_bindings.sh(docker with buildx; it builds a debug PHP 8.5 in a container and runs bindgen there) and commit the result. The lefthookbindingsjob runs it whenallowed_bindings.rsis staged. Never editdocsrs_bindings.rsby hand; the docs.rs build of a symbol that is missing there fails. The regeneration also picks up upstream changes to symbols already listed (new fields inzend_executor_globals, …). Commit the whole file: thedocsrs bindingsworkflow reruns the script and fails on any diff. - Rust code that uses a symbol only some PHP versions have is gated with the version cfgs (below), in the bindings and at each use.
cargo php stubsloads the extension without PHP:crates/cli/src/main.rsdefines a null stub for each name inallowed_bindings.rs, and real allocator stubs (_emalloc,_femalloc, …). A PHP patch release can make a Zend inline thatsrc/wrapper.cuses reference a new symbol, andstubs_snapshotthen fails withundefined symbol: <name>. Find that inline in the php-src headers and add a real stub next to_emalloc.
Never declare a Zend function in a private extern "C" block or hard-code a struct
layout to avoid the allowlist: the layout differs across versions, ZTS and platforms,
and bindgen is what keeps it right.
Version and build cfgs
php80…php86: stacked, so a PHP 8.4 build hasphp80…php84. Gate code that needs a newer API with#[cfg(php84)], and the older path with#[cfg(not(php84))]. The minimum is enforced inbuild.rs(min_api_version): PHP 8.1 with the defaultenumfeature, else 8.0 (with an EOL warning).php_zts: thread-safe PHP. Globals go through TSRM (ext_php_rs_executor_globals()and friends), never a directstatic.php_debug: a--enable-debugPHP (ZEND_DEBUG); struct layouts and some assertions differ.- In C (
wrapper.c/.h) the same versions areEXT_PHP_RS_PHP_8xdefines. - New cfgs must be declared through
ext_php_rs_build::emit_check_cfg(); crates that use them (liketests/) declare them in[lints.rust] unexpected_cfgs. EXT_PHP_RS_STATIC_EXT=1builds for static linking into the PHP binary (guideadvanced/static_linking.md); CI only compile-checks it.cargo testcan't work in that mode:wrapper.cthen references_tsrm_ls_cache, which a prebuiltlibphpdoesn't export, so test executables fail to link. A functional check needs the two-pass php-src build of the guide.
Supporting a new PHP version
Precedent: "feat(php): support PHP 8.6" (#833) and "accept every PHP 8.6 API version up to 20261112" (#841). The usual set of changes:
crates/php-build/src/lib.rs: theApiVersionvariant with its API number (PHP_API_VERSION/ZEND_MODULE_API_NOof the release,YYYY_MM_DD),max(),versions(),cfg_name,define_name, theTryFrom<u32>ranges andMAX_API_VERSION(pre-releases bump the API number: accept the whole range up to the RC's number);- the Zend API differences in
src/behind the new cfg (renamed fields, new parameters, removed functions), andallowed_bindings.rs+ regenerateddocsrs_bindings.rsfor new symbols; windows_build.rsif the devel pack name or URL scheme changed;- CI: the PHP version in the
build.ymlmatrix (with the exclusions for releases that have no Windows devel pack yet),.github/action/musl/Dockerfileand the musl matrix, the nixphp8xshells inflake.nix(while nixpkgs lags behind, overrideversionandphpSrcwith the php-src tag, asphp86Pkgdoes); - the docs.rs bindings and the
DOCS_RScfgs inbuild.rs, only at GA:tools/update_bindings.shdownloads php.net releases, not RCs; - the guide pages that list version-specific behaviour.
README.mdonly states the minimum ("PHP 8.1 or later"), which changes when the oldest version is dropped (withmin_api_versioninbuild.rs).
Run the version's build in a matching environment (nix .#php86, docker, or CI);
then let the full CI matrix run.
Engine traps
- At MINIT,
EG(class_table)is still null:init_executor()sets it at request startup. Look classes up inCG(class_table), assrc/builders/class.rsdoes. A second internal class with the same name silently replaces the first (zend_hash_update_ptr). - Read the php-src code of a status getter before you trust it.
php_output_get_status()masks outPHP_OUTPUT_ACTIVATEDon PHP 8.1 to 8.5, soext_php_rs_output_activated()insrc/wrapper.creadsOG(flags)itself.
Windows
- Windows PHP uses
vectorcallforZEND_FASTCALLfunctions, so Windows builds need nightly (#![cfg_attr(windows, feature(abi_vectorcall))]in every crate that defines extension functions;zend_fastcall!picks the ABI).wrapper.hundefines__clang__aroundzend_portability.hso bindgen sees the MSVC definition. - Windows-only failures in CI are real. Don't gate code off on Windows to make one go away; find the ABI, layout or linker difference.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

