Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active February 24, 2026 23:07
Show Gist options
  • Select an option

  • Save masakielastic/548290e5b00b3dfb1a46682a92dde55c to your computer and use it in GitHub Desktop.

Select an option

Save masakielastic/548290e5b00b3dfb1a46682a92dde55c to your computer and use it in GitHub Desktop.
クロージャ | PHP 拡張 ext-php-rs

クロージャ | PHP 拡張 ext-php-rs

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment