Attribute Macro ext_php_rs::php_impl [−][src]
#[php_impl]
Expand description
Annotates a structs impl
block, declaring that all methods and constants declared
inside the impl
block will be declared as PHP methods and constants.
If you do not want to export a method to PHP, declare it in another impl
block that is not
tagged with this macro.
The declared methods and functions are kept intact so they can continue to be called from Rust.
Methods do generate an additional function, with an identifier in the format
_internal_php_#ident
.
Methods and constants are declared mostly the same as their global counterparts, so read the
documentation on the php_function
and php_const
macros for more details.
The main difference is that the contents of the impl
block do not need to be tagged with
additional attributes - this macro assumes that all contents of the impl
block are to be
exported to PHP.
The only contrary to this is setting the visibility, optional argument and default arguments for methods. These are done through seperate macros:
#[defaults(key = value, ...)]
for setting defaults of method variables, similar to the function macro. Arguments with defaults need to be optional.#[optional(key)]
for settingkey
as an optional argument (and therefore the rest of the arguments).#[public]
,#[protected]
and#[private]
for setting the visibility of the method, defaulting to public. The Rust visibility has no effect on the PHP visibility.
Methods can take a immutable or a mutable reference to self
, but cannot consume self
. They
can also take no reference to self
which indicates a static method.
Example
#[php_class]
#[derive(Debug, Default)]
pub struct Human {
name: String,
age: i32,
}
#[php_impl]
impl Test {
// Class constant - `Human::AGE_LIMIT`
const AGE_LIMIT: i32 = 100;
#[optional(age)]
#[default(age = 0)]
pub fn __construct(&mut self, name: String, age: i32) {
self.name = name;
self.age = age;
}
pub fn get_name(&self) -> String {
self.name.clone()
}
pub fn get_age(&self) -> i32 {
self.age
}
// Static method - `Human::get_age_limit()`
pub fn get_age_limit() -> i32 {
Self::AGE_LIMIT
}
}