Skip to content

Commit 855b49a

Browse files
committed
feat: Add doc about interface in guide
1 parent 473f628 commit 855b49a

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

guide/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [Macros](./macros/index.md)
2828
- [Module](./macros/module.md)
2929
- [Function](./macros/function.md)
30+
- [Interfaces](./macros/interface.md)
3031
- [Classes](./macros/classes.md)
3132
- [`impl`s](./macros/impl.md)
3233
- [Constants](./macros/constant.md)

guide/src/macros/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ used from PHP without fiddling around with zvals.
1313
methods and constants.
1414
- [`php_const`] - Used to export a Rust constant to PHP as a global constant.
1515
- [`php_extern`] - Attribute used to annotate `extern` blocks which are deemed as
16+
- [`php_interface`] - Attribute used to export Rust Trait to PHP interface
1617
PHP functions.
1718
- [`php`] - Used to modify the default behavior of the above macros. This is a
1819
generic attribute that can be used on most of the above macros.
@@ -23,4 +24,5 @@ used from PHP without fiddling around with zvals.
2324
[`php_impl`]: ./impl.md
2425
[`php_const`]: ./constant.md
2526
[`php_extern`]: ./extern.md
27+
[`php_interface`]: ./interface.md
2628
[`php`]: ./php.md

guide/src/macros/interface.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# `#[php_interface]` Attribute
2+
3+
You can export an entire `Trait` block to PHP. This exports all methods as well
4+
as constants to PHP on the interface. Trait method SHOULD NOT contain default implementation
5+
6+
## Options
7+
8+
By default all constants are renamed to `UPPER_CASE` and all methods are renamed to
9+
camelCase. This can be changed by passing the `change_method_case` and
10+
`change_constant_case` as `#[php]` attributes on the `impl` block. The options are:
11+
12+
- `#[php(change_method_case = "snake_case")]` - Renames the method to snake case.
13+
- `#[php(change_constant_case = "snake_case")]` - Renames the constant to snake case.
14+
15+
See the [`name` and `change_case`](./php.md#name-and-change_case) section for a list of all
16+
available cases.
17+
18+
## Methods
19+
20+
See the [php_impl](./impl.md#)
21+
22+
## Constants
23+
24+
See the [php_impl](./impl.md#)
25+
26+
## Example
27+
28+
Define trait example with few methods and constant, and try implement this interface
29+
in php
30+
31+
```rust,no_run
32+
# #![cfg_attr(windows, feature(abi_vectorcall))]
33+
# extern crate ext_php_rs;
34+
use ext_php_rs::{prelude::*, types::ZendClassObject};
35+
36+
37+
#[php_interface]
38+
#[php(name = "Rust\\TestInterface")]
39+
trait Test {
40+
const TEST: &'static str = "TEST";
41+
42+
fn co();
43+
44+
#[php(defaults(value = 0))]
45+
fn set_value(&mut self, value: i32);
46+
}
47+
48+
#[php_module]
49+
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
50+
module
51+
.interface::<PhpInterfaceTest>()
52+
}
53+
54+
# fn main() {}
55+
```
56+
57+
Using our newly created interface in PHP:
58+
59+
```php
60+
<?php
61+
62+
assert(interface_exists("Rust\TestInterface"));
63+
64+
class B implements Rust\TestInterface {
65+
66+
public static function co() {}
67+
68+
public function setValue(?int $value = 0) {
69+
70+
}
71+
}
72+
73+
```

tests/src/integration/interface/interface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
final class Test extends Exception implements ExtPhpRs\Interface\EmptyObjectInterface
1111
{
12-
public function __construct()
13-
{
14-
}
15-
1612
public static function void(): void
1713
{
1814
}

0 commit comments

Comments
 (0)