A library that allows us to write PHP extensions using pure Rust and using safe Rust whenever possible.
libclang version >= 9
php version >= 7
os
- linux
php
version
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
- 8.0
mode
- nts
sapi
- cli
- Make sure
libclang
andphp
is installed.
# If you are using debian like linux system:
sudo apt install libclang-10-dev php-cli
- Create you cargo project, suppose your application is called myapp.
cargo new myapp
- Add the dependencies and metadata to you Cargo project.
[lib]
crate-type = ["cdylib"]
[dependencies]
phper = "0.2"
- Add these code to
main.rs
.
use phper::cmd::make;
fn main() {
make();
}
- Write you owned extension logic in
lib.rs
.
#[php_get_module]
pub fn get_module(module: &mut Module) {
// set module metadata
module.set_name(env!("CARGO_PKG_NAME"));
module.set_version(env!("CARGO_PKG_VERSION"));
module.set_author(env!("CARGO_PKG_AUTHORS"));
// ...
}
- Build and install, if your php isn't installed globally, you should specify the path of
php-config
.
# 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, if you install php globally, you should use sudo.
cargo run --release -- install
- Edit your
php.ini
, add the below line.
extension = myapp
- Enjoy.
See examples.