You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guide/src/macros/impl.md
+131Lines changed: 131 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,8 @@ implementations cannot be exported to PHP.
8
8
If you do not want a function exported to PHP, you should place it in a separate
9
9
`impl` block.
10
10
11
+
If you want to use async Rust, use `#[php_async_impl]`, instead: see [here »](#async) for more info.
12
+
11
13
## Methods
12
14
13
15
Methods basically follow the same rules as functions, so read about the
@@ -63,6 +65,20 @@ the attribute, the function is not exported to PHP like a regular method.
63
65
64
66
Constructors cannot use the visibility or rename attributes listed above.
65
67
68
+
### Async
69
+
70
+
Using `#[php_async_impl]` instead of `#[php_impl]` allows us to expose any async Rust library to PHP, using [PHP fibers](https://www.php.net/manual/en/language.fibers.php), [php-tokio](https://github.com/danog/php-tokio) and the [PHP Revolt event loop](https://revolt.run) under the hood to handle async interoperability.
71
+
72
+
This allows full compatibility with [amphp](https://amphp.org), [PSL](https://github.com/azjezz/psl), [reactphp](https://reactphp.org) and any other async PHP library based on [Revolt](https://revolt.run).
73
+
74
+
Traits annotated with `#[php_impl]` can freely expose any async function, using `await` and any async Rust library.
75
+
76
+
Make sure to also expose the `php_tokio::EventLoop::init` and `php_tokio::EventLoop::wakeup` functions to PHP in order to initialize the event loop, as specified in the full example [here &rauquo;](#async-example).
77
+
78
+
Also, make sure to invoke `EventLoop::shutdown` in the request shutdown handler to clean up the tokio event loop before finishing the request.
79
+
80
+
See [here &rauquo;](#async-example) for the full example.
81
+
66
82
## Constants
67
83
68
84
Constants are defined as regular Rust `impl` constants. Any type that implements
In this example, we're exposing an async Rust HTTP client library called [reqwest](https://docs.rs/reqwest/latest/reqwest/) to PHP, using [PHP fibers](https://www.php.net/manual/en/language.fibers.php), [php-tokio](https://github.com/danog/php-tokio) and the [PHP Revolt event loop](https://revolt.run) under the hood to handle async interoperability.
184
+
185
+
This allows full compatibility with [amphp](https://amphp.org), [PSL](https://github.com/azjezz/psl), [reactphp](https://reactphp.org) and any other async PHP library based on [Revolt](https://revolt.run).
Here's the async PHP code we use to interact with the Rust class we just exposed.
219
+
220
+
The `Client::init` method needs to be called only once in order to initialize the Revolt event loop and link it to the Tokio event loop, as shwon by the following code.
221
+
222
+
See [here »](https://amphp.org) for more info on async PHP using [amphp](https://amphp.org) + [revolt](https://revolt.run).
0 commit comments