Open joehoyle opened 2 years ago
Seems there is more work to be done regarding extending PHP classes when having a Rust struct backing the new class. In the meantime for exceptions, can use the following example:
use ext_php_rs::{
builders::ClassBuilder,
exception::PhpResult,
prelude::*,
zend::{ce, ClassEntry},
};
static mut REDIS_EXCEPTION: Option<&'static ClassEntry> = None;
#[php_startup]
pub fn startup() {
let ce = ClassBuilder::new("Redis\\Exception\\RedisException")
.extends(ce::exception())
.build()
.expect("Failed to build RedisException");
unsafe { REDIS_EXCEPTION.replace(ce) };
}
#[php_function]
pub fn get_redis_exc() -> PhpResult {
Err(PhpException::new("noo!".into(), 0, unsafe {
REDIS_EXCEPTION.expect("did not set exception ce")
}))
}
#[php_module]
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
module
}
Ah, thanks so much for the workaround @davidcole1340 !
When running the example from https://github.com/davidcole1340/ext-php-rs/blob/466c1658e3c91db3398c585a3b859b18e5b8e070/guide/src/macros/classes.md#example in returning an exception using
PhpException::from_class::<RedisException>
, I'm seeing a panic: