PHPER

PHP Enjoy Rust

Rust ❤️ PHP

The framework that allows us to write PHP extensions using pure and safe Rust whenever possible.

$ cargo add phper

Features

Pure and safe Rust

Register functions, classes, interfaces, enums, constants and ini settings with safe Rust APIs instead of C macros.

PHP 7.0 to 8.5

Supports PHP 7.0 to 8.5, NTS and ZTS builds, with or without debug.

Linux and macOS

Runs on Linux and macOS, in both the CLI and FPM SAPIs.

The Rust ecosystem

Bring crates from crates.io to PHP, like reqwest for an HTTP client.

Hello world

A PHP function written in Rust. The full walkthrough is in the quick start.

src/lib.rs
use phper::{
    echo, functions::Argument, modules::Module, php_get_module, values::ZVal,
};

fn say_hello(arguments: &mut [ZVal]) -> phper::Result<()> {
    let name = arguments[0].expect_z_str()?.to_str()?;
    echo!("Hello, {}!\n", name);
    Ok(())
}

#[php_get_module]
pub fn get_module() -> Module {
    let mut module = Module::new(
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION"),
        env!("CARGO_PKG_AUTHORS"),
    );
    module
        .add_function("say_hello", say_hello)
        .argument(Argument::new("name"));
    module
}
shell
$ cargo build
$ php -d "extension=target/debug/libhello.so" \
      -r "say_hello('Bob');"
Hello, Bob!

Requirements

  • rust 1.85 or later
  • libclang 9.0 or later
  • php 7.0 or later

Documentation

API reference on docs.rs, examples on GitHub.

Projects using PHPER