|
| 1 | +# Register interface |
| 2 | + |
| 3 | +Registering interfaces is similar of registering classes. |
| 4 | + |
| 5 | +First, you have to new the class builder |
| 6 | +[`InterfaceEntity`](phper::classes::InterfaceEntity), |
| 7 | +then extends interfaces (if there are), |
| 8 | +add public abstract methods, finally add it into the `Module`. |
| 9 | + |
| 10 | +Here is the simplest example: |
| 11 | + |
| 12 | +```rust,no_run |
| 13 | +use phper::{classes::InterfaceEntity, modules::Module, php_get_module}; |
| 14 | +
|
| 15 | +#[php_get_module] |
| 16 | +pub fn get_module() -> Module { |
| 17 | + let mut module = Module::new( |
| 18 | + env!("CARGO_CRATE_NAME"), |
| 19 | + env!("CARGO_PKG_VERSION"), |
| 20 | + env!("CARGO_PKG_AUTHORS"), |
| 21 | + ); |
| 22 | +
|
| 23 | + let foo = InterfaceEntity::new("Foo"); |
| 24 | +
|
| 25 | + module.add_interface(foo); |
| 26 | +
|
| 27 | + module |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Just like these codes in PHP: |
| 32 | + |
| 33 | +```php |
| 34 | +<?php |
| 35 | + |
| 36 | +interface Foo {} |
| 37 | +``` |
| 38 | + |
| 39 | +## Extends interfaces |
| 40 | + |
| 41 | +If you want the interface `Foo` extends `ArrayAccess` and `Iterator` interfaces. |
| 42 | + |
| 43 | +```rust,no_run |
| 44 | +use phper::classes::{InterfaceEntity, ClassEntry}; |
| 45 | +use phper::classes::{array_access_class, iterator_class}; |
| 46 | +
|
| 47 | +let mut foo = InterfaceEntity::new("Foo"); |
| 48 | +foo.extends(|| array_access_class()); |
| 49 | +foo.extends(|| iterator_class()); |
| 50 | +``` |
| 51 | + |
| 52 | +Same as: |
| 53 | + |
| 54 | +```php |
| 55 | +<?php |
| 56 | + |
| 57 | +interface Foo extends ArrayAccess, Iterator {} |
| 58 | +``` |
| 59 | + |
| 60 | +## Add methods |
| 61 | + |
| 62 | +Interface can add public abstract methods. |
| 63 | + |
| 64 | +```rust,no_run |
| 65 | +use phper::classes::{InterfaceEntity, ClassEntry, Visibility}; |
| 66 | +use phper::functions::Argument; |
| 67 | +use phper::objects::StateObj; |
| 68 | +use phper::values::ZVal; |
| 69 | +
|
| 70 | +let mut foo = InterfaceEntity::new("Foo"); |
| 71 | +foo.add_method("doSomethings").argument(Argument::by_val("name")); |
| 72 | +``` |
| 73 | + |
| 74 | +Note that abstract has no method body, so you don't need to add the handler to the method. |
0 commit comments