Cargo.toml
[package]
name = "closure-project"
version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib"]
[dependencies]
ext-php-rs = "0.15"src/lib.rs
use ext_php_rs::prelude::*;
use ext_php_rs::types::{ZendCallable, Zval};
#[php_function]
pub fn call_with_42(callback: ZendCallable) -> PhpResult<Zval> {
let arg: i64 = 42;
// try_call は「参照」を渡す
let result = callback.try_call(vec![&arg])?;
Ok(result)
}
#[php_module]
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
module.function(wrap_function!(call_with_42))
}cargo build
php -d extension=target/debug/libphp_warp_server.so test.php
test.php
<?php
echo call_with_42(fn(int $x) => $x * 2), PHP_EOL;