@@ -16,11 +16,10 @@ There are also additional macros that modify the class. These macros **must** be
16
16
placed underneath the ` #[php_class] ` attribute.
17
17
18
18
- ` #[extends(ce)] ` - Sets the parent class of the class. Can only be used once.
19
- ` ce ` must be a valid Rust expression when it is called inside the
20
- ` #[php_module] ` function.
19
+ ` ce ` must be a function with the signature ` fn() -> &'static ClassEntry ` .
21
20
- ` #[implements(ce)] ` - Implements the given interface on the class. Can be used
22
- multiple times. ` ce ` must be a valid Rust expression when it is called inside
23
- the ` #[php_module] ` function .
21
+ multiple times. ` ce ` must be a function with the signature
22
+ ` fn() -> &'static ClassEntry ` .
24
23
25
24
You may also use the ` #[prop] ` attribute on a struct field to use the field as a
26
25
PHP property. By default, the field will be accessible from PHP publicly with
@@ -67,18 +66,20 @@ This example creates a PHP class `Human`, adding a PHP property `address`.
67
66
``` rust,no_run
68
67
# #![cfg_attr(windows, feature(abi_vectorcall))]
69
68
# extern crate ext_php_rs;
70
- # use ext_php_rs::prelude::*;
69
+ use ext_php_rs::prelude::*;
70
+
71
71
#[php_class]
72
72
pub struct Human {
73
73
name: String,
74
74
age: i32,
75
75
#[prop]
76
76
address: String,
77
77
}
78
- # #[php_module]
79
- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
80
- # module
81
- # }
78
+
79
+ #[php_module]
80
+ pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
81
+ module.class::<Human>()
82
+ }
82
83
# fn main() {}
83
84
```
84
85
@@ -88,11 +89,14 @@ it in the `Redis\Exception` namespace:
88
89
``` rust,no_run
89
90
# #![cfg_attr(windows, feature(abi_vectorcall))]
90
91
# extern crate ext_php_rs;
91
- use ext_php_rs::prelude::*;
92
- use ext_php_rs::{exception::PhpException, zend::ce};
92
+ use ext_php_rs::{
93
+ prelude::*,
94
+ exception::PhpException,
95
+ zend::ce,
96
+ };
93
97
94
98
#[php_class(name = "Redis\\Exception\\RedisException")]
95
- #[extends(ce::exception() )]
99
+ #[extends(ce::exception)]
96
100
#[derive(Default)]
97
101
pub struct RedisException;
98
102
@@ -101,25 +105,33 @@ pub struct RedisException;
101
105
pub fn throw_exception() -> PhpResult<i32> {
102
106
Err(PhpException::from_class::<RedisException>("Not good!".into()))
103
107
}
104
- # #[php_module]
105
- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
106
- # module
107
- # }
108
+
109
+ #[php_module]
110
+ pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
111
+ module
112
+ .class::<RedisException>()
113
+ .function(wrap_function!(throw_exception))
114
+ }
108
115
# fn main() {}
109
116
```
110
117
111
118
## Implementing an Interface
112
119
113
- To implement an interface, use ` #[implements(ce)] ` where ` ce ` is an expression returning a ` ClassEntry ` .
120
+ To implement an interface, use ` #[implements(ce)] ` where ` ce ` is a function returning a ` ClassEntry ` .
114
121
The following example implements [ ` ArrayAccess ` ] ( https://www.php.net/manual/en/class.arrayaccess.php ) :
122
+
115
123
``` rust,no_run
116
124
# #![cfg_attr(windows, feature(abi_vectorcall))]
117
125
# extern crate ext_php_rs;
118
- use ext_php_rs::prelude::*;
119
- use ext_php_rs::{exception::PhpResult, types::Zval, zend::ce};
126
+ use ext_php_rs::{
127
+ prelude::*,
128
+ exception::PhpResult,
129
+ types::Zval,
130
+ zend::ce,
131
+ };
120
132
121
133
#[php_class]
122
- #[implements(ce::arrayaccess() )]
134
+ #[implements(ce::arrayaccess)]
123
135
#[derive(Default)]
124
136
pub struct EvenNumbersArray;
125
137
@@ -154,9 +166,10 @@ impl EvenNumbersArray {
154
166
Err("Setting values is not supported".into())
155
167
}
156
168
}
157
- # #[php_module]
158
- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
159
- # module
160
- # }
169
+
170
+ #[php_module]
171
+ pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
172
+ module.class::<EvenNumbersArray>()
173
+ }
161
174
# fn main() {}
162
175
```
0 commit comments