Skip to main content

static-php-py

"static PHP for Python" — a single PHP binary wrapped behind one Python class.

Run PHP scripts from Python without a system PHP install, binding one binary from a companion, a path, or a URL.

Install

pip install static-php-py            # code-only, bring your own binary
pip install "static-php-py[bundled]" # also pulls the companion PHP binary

The [bundled] extra pulls the companion distribution static-php-py-binary (internal, exact-pinned — never install it directly), supplying a pre-built PHP 8.4 binary for the host platform.

Usage

from static_php_py import PHP

php = PHP.bundled()                       # companion binary (needs [bundled] extra)
php = PHP.local("/usr/bin/php")           # explicit path
php = PHP.system()                        # first php on the system PATH
php = PHP.remote("https://example.com/php-8.4.tar.gz",   # download + cache
                 checksum="sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")

result = php.run("script.php", args=["--verbose"])
if result.success:
    print(result.stdout)
else:
    print(f"Error (code {result.return_code}): {result.stderr}")

print(php.eval("echo phpversion();").stdout)
print(php.version())

report = php.check_extensions(["tokenizer", "mbstring"])
if not report.satisfied:
    print(f"Missing: {report.missing}")

Required version and extensions

Declare the PHP version and extension set your package needs once, as a typed Requirements constant, and check any bound binary against it with require:

from static_php_py import PHP, Requirements

REQUIREMENTS = Requirements(
    version=">=8.4",                              # PEP 440 specifier, or None for any
    extensions=frozenset({"tokenizer", "mbstring"}),
)

php = PHP.bundled().require(REQUIREMENTS)

require probes only what the declaration states — no version subprocess without a version constraint, no php -m without extensions — and returns the same PHP for chaining. A violated version constraint raises VersionUnsatisfiedError; a missing extension raises ExtensionUnsatisfiedError, naming the static-php-build tool that produces a satisfying binary. The same constant drives static-php-build --manifest your_lib:REQUIREMENTS, so both tools stay in sync from one declaration.

Self-managed binary

A library that needs a fixed extension set ships its own binaries as package data and loads the host's one with PHP.local, so its end users install nothing and never compile. Build one archive per OS with static-php-build, declare the archives (.tar.gz/.tgz/.zip, default directory your_lib/_resources/) as package data, and resolve at runtime with PackagedBinary — never the system PATH or a user-supplied path. Selection is by name heuristics — the archive whose name carries the host's OS and architecture tokens (and the series, when given) is chosen — so official static-php-cli release names work unrenamed:

from static_php_py import PHP, PackagedBinary

_BINARY = PackagedBinary(package="your_lib", series="8.4")

def php() -> PHP:
    return PHP.local(_BINARY.resolve())

Full CI-to-wheel flow: ../../docs/llms/bundling-pipeline.md.

API

Classes

  • PHP — wrapper bound to one binary.
    • PHP.bundled() -> PHP — companion binary; raises BinaryNotFoundError naming the [bundled] extra when the companion is absent.
    • PHP.local(path: Path | str) -> PHP — explicit path.
    • PHP.system() -> PHP — first php executable on the system PATH.
    • PHP.remote(url: str, *, force: bool = False, timeout: float = 300.0, checksum: str | None = None) -> PHP — download, extract (.tar.gz/.tgz/.zip), and cache; checksum ("sha256:<hexdigest>", also sha384/sha512) verifies the download before use.
    • PHP.cache_dir() -> Path — platform-specific download cache directory.
    • require(requirements: Requirements) -> PHP — check the binary against a declaration; probes only declared parts; returns self.
    • run(script, args=None, *, input=None, ini=None, timeout=None) -> ExecutionResult — input feeds stdin; ini maps to -d key=value options.
    • eval(code, *, input=None, ini=None, timeout=None) -> ExecutionResult — same input/ini semantics as run.
    • version() -> str
    • extensions() -> frozenset[str] — loaded extensions via php -m, lowercased.
    • check_extensions(required) -> ExtensionReport — pure inspection; no download or build.
    • path (property) -> Path
  • Requirements(version: str | None = None, extensions: frozenset[str] = frozenset()) — frozen declaration of the needed PHP version (PEP 440 specifier) and extension set.
  • PackagedBinary(package: str, series: str | None = None, *, directory: str = "_resources") — resolver for a consumer-carried platform archive set; series narrows selection (None selects across all series), directory names the package-relative archive directory.
    • resolve() -> Path — select the host's archive by name heuristics (macos≈darwin, windows≈win, x86_64≈amd64, aarch64≈arm64; a name with no architecture token matches any), extract its PHP binary beside it (once), and return the path; pass the result to PHP.local.
  • ExecutionResult — frozen data holder: return_code, stdout, stderr, success.
  • ExtensionReport — frozen data holder: required, loaded, missing, satisfied.

Exceptions

  • PHPError — base.
  • BinaryNotFoundError, ExecutionError, DownloadError, InvalidArchiveError, ExtensionUnsatisfiedError, VersionUnsatisfiedError.

Full design: docs/design-api.md. Concepts: docs/concept.md. Doc index: docs/index.md.

For Agents

Agent-consumable documentation index at ../../docs/llms.txt (llmstxt.org format).

Citation

If you use this project in academic work, cite our paper. BibTeX (placeholder until the paper is released):

@misc{static_php_py_placeholder,
  title  = {{TODO: paper title — not yet released}},
  author = {{TODO: authors}},
  year   = {{TODO}},
  note   = {Paper not yet released. Citation entry will be updated on publication.}
}

License

GPL-2.0 — see LICENSE. Pre-built binaries originate from static-php-cli.

Metadata

Release files for static-php-py 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for static-php-py 0.2.0
File Size Uploaded Provenance Actions
static_php_py-0.2.0.tar.gz 73.1 kB Without provenance Details for static_php_py-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for static-php-py 0.2.0
File Interpreter ABI Platform Actions
static_php_py-0.2.0-py3-none-any.whl Python 3 none any Details for static_php_py-0.2.0-py3-none-any.whl

Total release size: 115.1 kB

Release files / static_php_py-0.2.0.tar.gz

Download URL static_php_py-0.2.0.tar.gz
Size 73.1 kB
Tags Source
SHA-256 checksum
How to use checksums
025b051483e4301033530860b882370acb36c4e6fa6d420f0404ecb226e65230
BLAKE2b-256 checksum
How to use checksums
4f5c3a49718f426cf57dae0e03fda1bc549ac0c6f6ba1b09e5a34d4f73c0e719
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / static_php_py-0.2.0-py3-none-any.whl

Download URL static_php_py-0.2.0-py3-none-any.whl
Size 42.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17514f8465314ca4e657eea17e87f308bd24dc4073b4989a06e4b5a5022f9f8d
BLAKE2b-256 checksum
How to use checksums
b49e4f74d5a139f319c40fe857b0a99654a52abfdd4d41f7501684f92c1f369d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for static-php-py 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page