Skip to content

StateClass cannot be used for classes that are not created by phper. #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jmjoy opened this issue Apr 3, 2025 · 0 comments
Closed

StateClass cannot be used for classes that are not created by phper. #195

jmjoy opened this issue Apr 3, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@jmjoy
Copy link
Member

jmjoy commented Apr 3, 2025

          In fact, I prefer to change it this way to be consistent with the `implements` method. What do you think?

master...jmjoy:phper-fork:extends

Originally posted by @jmjoy in #190 (comment)

Previously, I mentioned that it was possible to add a StateClass::from_name method, but I realized that I made a critical mistake. The issue is that StateClass creates a StateObject using new_object, and the state held by StateObject is of type *mut dyn Any, which is the fixed state held by phper. However, if a non-phper class also creates a StateObject using new_object, problems will arise—for example, the code below will result in a segmentation fault:

#[php_get_module]
pub fn get_module() -> Module {
    // New `Module` with extension info.
    let mut module = Module::new(
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION"),
        env!("CARGO_PKG_AUTHORS"),
    );

    module.on_request_init(|| {
        let ex = StateClass::from_name("Exception");
        let ex_obj = ex.new_object([]).unwrap();
        let state = ex_obj.as_state();
    });

    module
}

I need to rethink the design of the ClassEntity::extends method to avoid this issue.

@jmjoy jmjoy added the bug Something isn't working label Apr 3, 2025
@jmjoy jmjoy changed the title In fact, I prefer to change it this way to be consistent with the implements method. What do you think? StateClass cannot be used for classes that are not created by phper. Apr 3, 2025
jmjoy added a commit to jmjoy/phper-fork that referenced this issue Apr 3, 2025
@jmjoy jmjoy closed this as completed Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant