Skip to content

Instantly share code, notes, and snippets.

@Christian-Rades
Created November 25, 2022 18:05
Show Gist options
  • Save Christian-Rades/66983ca9de0b817fa52f415c882c04a8 to your computer and use it in GitHub Desktop.
Save Christian-Rades/66983ca9de0b817fa52f415c882c04a8 to your computer and use it in GitHub Desktop.
PHP ext rs call_user_func with object as param
#![cfg_attr(windows, feature(abi_vectorcall))]
use ext_php_rs::{prelude::*, types::{Zval, ZendObject}, flags::DataType, ffi::_zval_struct, convert::IntoZval, call_user_func};
#[php_function]
pub fn cname(obj: &mut Zval) -> String {
let f = "get_class".into_zval(false).unwrap();
let param = ObjAsParamHack{inner: obj.shallow_clone()};
//let param = obj.shallow_clone();
//let param = ZendObject::from(obj.???);
let result = call_user_func!(f, param).unwrap();
result.string().unwrap()
}
struct ObjAsParamHack{inner: Zval}
impl Clone for ObjAsParamHack {
fn clone(&self) -> Self {
Self { inner: self.inner.shallow_clone() }
}
}
impl IntoZval for ObjAsParamHack {
const TYPE: DataType = DataType::Reference;
fn into_zval(self, _persistend: bool) -> Result<_zval_struct, ext_php_rs::error::Error> {
Ok(self.inner)
}
fn set_zval(self, zv: &mut Zval, _persistent: bool) -> Result<(), ext_php_rs::error::Error> {
let Self { inner } = self;
*zv = inner;
Ok(())
}
}
// Required to register the extension with PHP.
#[php_module]
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
module
}
<?php
class DTO {}
echo cname(new DTO()) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment