pub trait IntoConst: Debug {
// Required methods
fn stub_value(&self) -> String;
fn register_constant_flags(
&self,
name: &str,
module_number: i32,
flags: GlobalConstantFlags,
) -> Result<()>;
// Provided method
fn register_constant(&self, name: &str, module_number: i32) -> Result<()> { ... }
}Expand description
Implemented on types which can be registered as a constant in PHP.
Required Methods§
Sourcefn stub_value(&self) -> String
fn stub_value(&self) -> String
Returns the PHP stub representation of this constant value.
This is used when generating PHP stub files for IDE autocompletion.
The returned string should be a valid PHP literal (e.g., "hello",
42, true).
Sourcefn register_constant_flags(
&self,
name: &str,
module_number: i32,
flags: GlobalConstantFlags,
) -> Result<()>
fn register_constant_flags( &self, name: &str, module_number: i32, flags: GlobalConstantFlags, ) -> Result<()>
Registers a global module constant in PHP, with the value as the content of self. This function must be called in the module startup function, which is called after the module is initialized. The second parameter of the startup function will be the module number. This function allows you to pass any extra flags in if you require. Note that the case-sensitive and persistent flags are not set when you use this function, you must set these yourself.
Returns a result containing nothing if the constant was successfully registered.
§Parameters
name- The name of the constant.module_number- The module number that we are registering the constant under.flags- Flags to register the constant with.
§Errors
Returns an error if the constant flags could not be registered.
§Examples
use ext_php_rs::{constant::IntoConst, flags::GlobalConstantFlags};
pub extern "C" fn startup_function(_type: i32, module_number: i32) -> i32 {
42.register_constant_flags("MY_CONST_NAME", module_number, GlobalConstantFlags::Persistent | GlobalConstantFlags::Deprecated);
0
}Provided Methods§
Sourcefn register_constant(&self, name: &str, module_number: i32) -> Result<()>
fn register_constant(&self, name: &str, module_number: i32) -> Result<()>
Registers a global module constant in PHP, with the value as the content of self. This function must be called in the module startup function, which is called after the module is initialized. The second parameter of the startup function will be the module number. By default, the case-insensitive and persistent flags are set when registering the constant.
Returns a result containing nothing if the constant was successfully registered.
§Parameters
name- The name of the constant.module_number- The module number that we are registering the constant under.
§Errors
Returns an error if the constant could not be registered.
§Examples
use ext_php_rs::constant::IntoConst;
pub extern "C" fn startup_function(_type: i32, module_number: i32) -> i32 {
5.register_constant("MY_CONST_NAME", module_number); // MY_CONST_NAME == 5
"Hello, world!".register_constant("STRING_CONSTANT", module_number); // STRING_CONSTANT == "Hello, world!"
0
}