Attribute Macro ext_php_rs::php_module

source ·
#[php_module]
Expand description

Annotates a function that will be used by PHP to retrieve information about the module.

In the process, the function is wrapped by an extern "C" function which is called from PHP, which then calls the given function.

As well as wrapping the function, the ModuleBuilder is initialized and functions which have already been declared with the php_function attribute will be registered with the module, so ideally you won’t have to do anything inside the function.

The attribute must be called on a function last, i.e. the last proc-macro to be compiled, as the attribute relies on all other PHP attributes being compiled before the module. If another PHP attribute is compiled after the module attribute, an error will be thrown.

Note that if the function is not called get_module, it will be renamed.

If you have defined classes using the php_class macro and you have not defined a startup function, it will be automatically declared and registered.

§Example

The get_module function is required in every PHP extension. This is a bare minimum example, since the function is declared above the module it will automatically be registered when the module attribute is called.

#[php_function]
pub fn hello(name: String) -> String {
    format!("Hello, {}!", name)
}

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