#[php_impl_interface]Expand description
§#[php_impl_interface] Attribute
Marks a trait implementation as implementing a PHP interface. This allows
Rust structs marked with #[php_class] to implement Rust traits marked
with #[php_interface], and have PHP recognize the relationship.
Key feature: The macro automatically registers the trait methods as PHP
methods on the class. You don’t need to duplicate them in a separate
#[php_impl] block.
§Usage
use ext_php_rs::prelude::*;
#[php_interface]
trait MyInterface {
fn my_method(&self) -> String;
}
#[php_class]
struct MyClass;
// The trait method my_method() is automatically registered as a PHP method
#[php_impl_interface]
impl MyInterface for MyClass {
fn my_method(&self) -> String {
"Hello from MyClass!".to_string()
}
}
#[php_module]
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
module
.interface::<PhpInterfaceMyInterface>()
.class::<MyClass>()
}After registration, PHP’s is_a($obj, 'MyInterface') will return true
for instances of MyClass, and $obj->myMethod() will be callable.
§Method names
Each method is registered under the name that #[php_interface] gave it.
The trait’s change_method_case and the name or change_case of a trait
method apply to the class too. The macro takes no arguments:
#[php_impl_interface(change_method_case = "...")] is a compile error.
§Requirements
- The trait must be marked with
#[php_interface] - The struct must be marked with
#[php_class] - The interface must be registered before the class in the module builder