PHPER Framework用纯 Rust 开发 PHP 扩展

联合创作 · 2023-10-01 09:14

这是一个支持用纯 Rust 开发 PHP 扩展的框架,可以有效利用Rust生态,主要是对Zend API的封装。

PHPER 的意思是 PHP Enjoy Rust。

文档

https://docs.rs/phper

教程

https://docs.rs/phper-doc/

示例

use phper::{echo, functions::Argument, modules::Module, php_get_module, values::ZVal};

/// The php function, receive arguments with type `ZVal`.
fn say_hello(arguments: &mut [ZVal]) -> phper::Result<()> {
    // Get the first argument, expect the type `ZStr`, and convert to Rust utf-8
    // str.
    let name = arguments[0].expect_z_str()?.to_str()?;

    // Macro which do php internal `echo`.
    echo!("Hello, {}!\n", name);

    Ok(())
}

/// This is the entry of php extension, the attribute macro `php_get_module`
/// will generate the `extern "C" fn`.
#[php_get_module]
pub fn get_module() -> Module {
    // New `Module` with extension info.
    let mut module = Module::new(
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION"),
        env!("CARGO_PKG_AUTHORS"),
    );

    // Register function `say_hello`, with one argument `name`.
    module.add_function("say_hello", say_hello).argument(Argument::by_val("name"));

    module
}
浏览 5
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报