ext-php-rs icon indicating copy to clipboard operation
ext-php-rs copied to clipboard

Err: Attempted to access uninitialized class object

Open godruoyi opened this issue 2 years ago • 0 comments

When I want to custom the exception use the bellow code, that's copy from https://davidcole1340.github.io/ext-php-rs/macros/classes.html#example

#![cfg_attr(windows, feature(abi_vectorcall))]
extern crate ext_php_rs;

use ext_php_rs::prelude::*;
use ext_php_rs::{exception::PhpException, zend::ce};

#[php_class(name = "Redis\\Exception\\RedisException")]
#[extends(ce::exception())]
#[derive(Default)]
pub struct RedisException;

// Throw our newly created exception
#[php_function]
pub fn throw_exception() -> PhpResult<i32> {
    Err(PhpException::from_class::<RedisException>("Not good!".into()))
}

#[php_module]
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
    module
}

tests/Unit/BasicTest.php

it('throw_exception', function () {
    throw_exception();
})->throws('Redis\Exception\RedisException', 'Not good!');

I get an error,

$ cargo build
   Compiling myproject-php v0.1.0 (myproject)
    Finished dev [unoptimized + debuginfo] target(s) in 1.19s
    
$ php -d extension=../../target/debug/libmyproject_php.dylib vendor/bin/pest tests/Unit/BasicTest.php

thread '<unnamed>' panicked at 'Attempted to access uninitialized class object', ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ext-php-rs-0.10.1/src/types/class_object.rs:245:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
fish: Job 1, 'php -d extension=../../target/d…' terminated by signal SIGABRT (Abort)

Do you have any suggestions for me? I also want to create a PR to fix it, but I need some context or references first.

godruoyi avatar Aug 04 '23 05:08 godruoyi