Skip to content
/ phper Public
forked from phper-framework/phper

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

License

Notifications You must be signed in to change notification settings

azjezz/phper

 
 

Repository files navigation

PHPER (PHP Enjoy Rust)

CI Crates Docs Lines License

Rust ❤️ PHP

The framework that allows us to write PHP extensions using pure and 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.

    # If you are using debian like linux system:
    sudo apt install llvm-10-dev libclang-10-dev php-cli
  2. Create you cargo project, suppose your application is called myapp.

    cargo new myapp
  3. Add the dependencies and metadata to you Cargo project.

    [lib]
    crate-type = ["cdylib"]
    
    [dependencies]
    phper = "<LATEST VERSION>"
  4. Add these code to main.rs.

    use phper::cmd::make;
    
    fn main() {
        make();
    }
  5. Create the build.rs ( Adapting MacOS ).

    fn main() {
       #[cfg(target_os = "macos")]
       {
          println!("cargo:rustc-link-arg=-undefined");
          println!("cargo:rustc-link-arg=dynamic_lookup");
       }
    }
  6. 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
    }
  7. Build and install, if your php isn't installed globally, you should specify the path of php-config.

    # Optional, specify if php isn't installed globally.
    # export PHP_CONFIG=<Your path of php-config>
    
    # Build libmyapp.so.
    cargo build --release
    
    # Install to php extension path.
    cargo run --release -- install
    # Or if you install php globally, you should use sudo.
    # sudo ./target/release/myapp install
    
  8. Edit your php.ini, add the below line.

    extension = myapp
  9. Enjoy.

Examples

See examples.

The projects using PHPER

  • apache/skywalking-php - The PHP Agent for Apache SkyWalking, which provides the native tracing abilities for PHP project.

License

MulanPSL-2.0.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 90.2%
  • PHP 5.5%
  • C 4.3%