Crate phper

source · []
Expand description

PHPer

CI Crates Docs Lines License

Rust ❤️ PHP

A library that allows us to write PHP extensions using pure Rust and using safe Rust whenever possible.

Requirement

Necessary

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

Tested Support

  • OS
    • linux
    • macos
    • windows
  • PHP
    • version
      • 7.0
      • 7.1
      • 7.2
      • 7.3
      • 7.4
      • 8.0
      • 8.1
    • mode
      • nts
      • zts
    • sapi
      • cli
      • fpm
    • debug
      • disable
      • enable

Usage

  1. Make sure libclang and php is installed.
sudo apt install llvm-10-dev libclang-10-dev php-cli
  1. Create you cargo project, suppose your application is called myapp.
cargo new myapp
  1. Add the dependencies and metadata to you Cargo project.
[lib]
crate-type = ["cdylib"]

[dependencies]
phper = "0.3"
  1. Add these code to main.rs.
use phper::cmd::make;

fn main() {
    make();
}
  1. Write you owned extension logic in lib.rs.
use phper::{php_get_module, modules::Module};

#[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
}
  1. Build and install, if your php isn’t installed globally, you should specify the path of php-config.
export PHP_CONFIG=<Your path of php-config>

cargo build --release

cargo run --release -- install
  1. Edit your php.ini, add the below line.
extension = myapp
  1. Enjoy.

Examples

See examples.

License

Unlicense.

Re-exports

pub use crate::errors::Error;
pub use crate::errors::Result;
pub use phper_alloc as alloc;
pub use phper_sys as sys;

Modules

Command tools for build, test and install extension process.

The errors for crate and php.

Logs and echo facilities.

Apis relate to PHP types.

Apis relate to crate::sys::zval.

Macros

C style string end with ‘\0’.

C style string end with ‘\0’.

Equivalent to the php CG.

PHP deprecated logging.

PHP echo.

Equivalent to the php EG.

PHP error logging, will exit the request.

PHP notice logging.

PHP warning logging.

Attribute Macros

PHP module entry, wrap the phper::modules::Module write operation.

Derive Macros

Auto derive for phper::errors::Throwable.