Expand description

Hooks

PHP is a complex piece of machinery, and its lifecycle should be understood by anyone who wants to grasp how PHP operates.

Refer: https://www.phpinternalsbook.com/php7/extensions_design/php_lifecycle.html

PHP provides many hooks in lifecycle for extension to override.

There are MINIT, MSHUTDOWN, RINIT, RSHUTDOWN, GINIT, RSHUTDOWN.

Correspondingly, PHPER sets these hooks to complete some internal operations, such as registering extension information, functions, classes, constants, etc. However, it also exposes these hooks to users for overwriting.

The following presents the corresponding relationships between PHP hooks and Module methods:

PHP hooksModule method
MINITon_module_init
MSHUTDOWNon_module_shutdown
RINITon_request_init
RSHUTDOWNon_request_shutdown
use phper::{modules::Module, php_get_module};

#[php_get_module]
pub fn get_module() -> Module {
    let mut module = Module::new(
        env!("CARGO_CRATE_NAME"),
        env!("CARGO_PKG_VERSION"),
        env!("CARGO_PKG_AUTHORS"),
    );

    module.on_module_init(|| {
        // Do somethings in `MINIT` stage.
    });

    module
}