2
2
3
3
Structs can be exported to PHP as classes with the ` #[php_class] ` attribute
4
4
macro. This attribute derives the ` RegisteredClass ` trait on your struct, as
5
- well as registering the class to be registered with the ` #[php_module] ` macro.
5
+ well as registering the class to be registered with the ` #[php_module] ` containing
6
+ the class.
6
7
7
8
## Options
8
9
@@ -12,8 +13,7 @@ The attribute takes some options to modify the output of the class:
12
13
name is kept the same. If no name is given, the name of the struct is used.
13
14
Useful for namespacing classes.
14
15
15
- There are also additional macros that modify the class. These macros ** must** be
16
- placed underneath the ` #[php_class] ` attribute.
16
+ There are also additional macros that modify the class.
17
17
18
18
- ` #[extends(ce)] ` - Sets the parent class of the class. Can only be used once.
19
19
` ce ` must be a valid Rust expression when it is called inside the
@@ -23,7 +23,7 @@ placed underneath the `#[php_class]` attribute.
23
23
the ` #[php_module] ` function.
24
24
25
25
You may also use the ` #[prop] ` attribute on a struct field to use the field as a
26
- PHP property. By default, the field will be accessible from PHP publicly with
26
+ PHP property. By default, the field will then be accessible from PHP publicly with
27
27
the same name as the field. Property types must implement ` IntoZval ` and
28
28
` FromZval ` .
29
29
@@ -68,17 +68,16 @@ This example creates a PHP class `Human`, adding a PHP property `address`.
68
68
# #![cfg_attr(windows, feature(abi_vectorcall))]
69
69
# extern crate ext_php_rs;
70
70
# use ext_php_rs::prelude::*;
71
- #[php_class]
72
- pub struct Human {
73
- name: String,
74
- age: i32,
75
- #[prop]
76
- address: String,
71
+ #[php_module]
72
+ mod module {
73
+ #[php_class]
74
+ pub struct Human {
75
+ name: String,
76
+ age: i32,
77
+ #[prop]
78
+ address: String,
79
+ }
77
80
}
78
- # #[php_module]
79
- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
80
- # module
81
- # }
82
81
# fn main() {}
83
82
```
84
83
@@ -89,22 +88,22 @@ it in the `Redis\Exception` namespace:
89
88
# #![cfg_attr(windows, feature(abi_vectorcall))]
90
89
# extern crate ext_php_rs;
91
90
use ext_php_rs::prelude::*;
92
- use ext_php_rs::{exception::PhpException, zend::ce};
93
91
94
- #[php_class(name = "Redis\\Exception\\RedisException")]
95
- #[extends(ce::exception())]
96
- #[derive(Default)]
97
- pub struct RedisException;
92
+ #[php_module]
93
+ pub mod module {
94
+ use ext_php_rs::{exception::PhpException, zend::ce};
95
+
96
+ #[php_class(name = "Redis\\Exception\\RedisException")]
97
+ #[extends(ce::exception())]
98
+ #[derive(Default)]
99
+ pub struct RedisException;
98
100
99
- // Throw our newly created exception
100
- #[php_function]
101
- pub fn throw_exception() -> PhpResult<i32> {
102
- Err(PhpException::from_class::<RedisException>("Not good!".into()))
101
+ // Throw our newly created exception
102
+ #[php_function]
103
+ pub fn throw_exception() -> PhpResult<i32> {
104
+ Err(PhpException::from_class::<RedisException>("Not good!".into()))
105
+ }
103
106
}
104
- # #[php_module]
105
- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
106
- # module
107
- # }
108
107
# fn main() {}
109
108
```
110
109
@@ -116,47 +115,47 @@ The following example implements [`ArrayAccess`](https://www.php.net/manual/en/c
116
115
# #![cfg_attr(windows, feature(abi_vectorcall))]
117
116
# extern crate ext_php_rs;
118
117
use ext_php_rs::prelude::*;
119
- use ext_php_rs::{exception::PhpResult, types::Zval, zend::ce};
120
-
121
- #[php_class]
122
- #[implements(ce::arrayaccess())]
123
- #[derive(Default)]
124
- pub struct EvenNumbersArray;
125
-
126
- /// Returns `true` if the array offset is an even number.
127
- /// Usage:
128
- /// ```php
129
- /// $arr = new EvenNumbersArray();
130
- /// var_dump($arr[0]); // true
131
- /// var_dump($arr[1]); // false
132
- /// var_dump($arr[2]); // true
133
- /// var_dump($arr[3]); // false
134
- /// var_dump($arr[4]); // true
135
- /// var_dump($arr[5] = true); // Fatal error: Uncaught Exception: Setting values is not supported
136
- /// ```
137
- #[php_impl]
138
- impl EvenNumbersArray {
139
- pub fn __construct() -> EvenNumbersArray {
140
- EvenNumbersArray {}
141
- }
142
- // We need to use `Zval` because ArrayAccess needs $offset to be a `mixed`
143
- pub fn offset_exists(&self, offset: &'_ Zval) -> bool {
144
- offset.is_long()
145
- }
146
- pub fn offset_get(&self, offset: &'_ Zval) -> PhpResult<bool> {
147
- let integer_offset = offset.long().ok_or("Expected integer offset")?;
148
- Ok(integer_offset % 2 == 0)
149
- }
150
- pub fn offset_set(&mut self, _offset: &'_ Zval, _value: &'_ Zval) -> PhpResult {
151
- Err("Setting values is not supported".into())
152
- }
153
- pub fn offset_unset(&mut self, _offset: &'_ Zval) -> PhpResult {
154
- Err("Setting values is not supported".into())
118
+
119
+ #[php_module]
120
+ mod module {
121
+ use ext_php_rs::{exception::PhpResult, types::Zval, zend::ce};
122
+
123
+ #[php_class]
124
+ #[implements(ce::arrayaccess())]
125
+ #[derive(Default)]
126
+ pub struct EvenNumbersArray;
127
+
128
+ /// Returns `true` if the array offset is an even number.
129
+ /// Usage:
130
+ /// ```php
131
+ /// $arr = new EvenNumbersArray();
132
+ /// var_dump($arr[0]); // true
133
+ /// var_dump($arr[1]); // false
134
+ /// var_dump($arr[2]); // true
135
+ /// var_dump($arr[3]); // false
136
+ /// var_dump($arr[4]); // true
137
+ /// var_dump($arr[5] = true); // Fatal error: Uncaught Exception: Setting values is not supported
138
+ /// ```
139
+ #[php_impl]
140
+ impl EvenNumbersArray {
141
+ pub fn __construct() -> EvenNumbersArray {
142
+ EvenNumbersArray {}
143
+ }
144
+ // We need to use `Zval` because ArrayAccess needs $offset to be a `mixed`
145
+ pub fn offset_exists(&self, offset: &'_ Zval) -> bool {
146
+ offset.is_long()
147
+ }
148
+ pub fn offset_get(&self, offset: &'_ Zval) -> PhpResult<bool> {
149
+ let integer_offset = offset.long().ok_or("Expected integer offset")?;
150
+ Ok(integer_offset % 2 == 0)
151
+ }
152
+ pub fn offset_set(&mut self, _offset: &'_ Zval, _value: &'_ Zval) -> PhpResult {
153
+ Err("Setting values is not supported".into())
154
+ }
155
+ pub fn offset_unset(&mut self, _offset: &'_ Zval) -> PhpResult {
156
+ Err("Setting values is not supported".into())
157
+ }
155
158
}
156
159
}
157
- # #[php_module]
158
- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
159
- # module
160
- # }
161
160
# fn main() {}
162
161
```
0 commit comments