phper-framework / phper

The framework that allows us to write PHP extensions using pure and safe Rust whenever possible.
Other
290 stars 16 forks source link

能提供个demo如何修改 pdo 构造函数 这类的例子么 #159

Closed luffe339 closed 1 month ago

luffe339 commented 1 month ago

// 从全局查找 PHP 的 PDO 类 if let Ok(pdo_class) = ClassEntry::from_globals("PDO") { // 获取原始的 PDO 构造函数 if let Ok(original_constructor) = pdo_class.get_method("construct", Visibility::Public) { // 添加一个新的 construct 方法 pdo_class.add_method("_construct", Visibility::Public, move |this, arguments, | { // 进行自定义逻辑,比如拦截 PDO 数据库连接信息 if let Some(arg0) = arguments.get(0) { let dsn = arg0.as_str().unwrap_or(""); println!("Intercepted PDO::__construct with DSN: {}", dsn); }

            // 调用原始的构造函数
            original_constructor.call(this, arguments)?;

            // 可以在这里添加后续逻辑
            println!("Finished executing original PDO::__construct");

            Ok(())
        });
    } else {
        eprintln!("Failed to get PDO::__construct method.");
    }
} else {
    eprintln!("PDO class not found.");
}   
luffe339 commented 1 month ago

get_method 方法不存在

jmjoy commented 1 month ago

有一个思路,你可以尝试一下(我没试过):

https://docs.rs/phper-sys/0.13.1/phper_sys/type.zend_class_entry.html

修改zend_class_entry.constructor.internal_function.handler

luffe339 commented 1 month ago

有一个思路,你可以尝试一下(我没试过):

https://docs.rs/phper-sys/0.13.1/phper_sys/type.zend_class_entry.html

修改zend_class_entry.constructor.internal_function.handler 好的,谢谢,我试试,刚学rust