Skip to content

Implicit #[php_module] can be akward #131

@vodik

Description

@vodik

Something I've been wanting to do for a while is to remove support for the implicit #[php_module]. As it stands, you currently can just drop this at the bottom of your file and have everything register automatically:

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

Two problems arise quickly with larger projects:

Order matter

Since macros run in order, you must make sure that the module function is the last thing the build sees. If anything is created after the fact, the build will fail with very usefulness error messages.

Code splitting / rust modules are harder to use.

In one of my larger projects, I split code into a few files. I have to make sure my main lib.rs imports everything in order for it to build. This in turn interacts makes my main module harder to read when I have conditional features split over multiple files.

Alternative

Follow the similar pattern which pyo3 does: explicit registration. I think its more predictable:

#[php_module]
pub fn module(m: &Module) -> Result<()> {
    m.add_class::<MyClass>()?;
    Ok(())
}

That will allow module to appear anywhere in the project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions