22
33Structs can be exported to PHP as classes with the ` #[php_class] ` attribute
44macro. 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.
67
78## Options
89
@@ -12,8 +13,7 @@ The attribute takes some options to modify the output of the class:
1213 name is kept the same. If no name is given, the name of the struct is used.
1314 Useful for namespacing classes.
1415
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.
1717
1818- ` #[extends(ce)] ` - Sets the parent class of the class. Can only be used once.
1919 ` ce ` must be a valid Rust expression when it is called inside the
@@ -23,7 +23,7 @@ placed underneath the `#[php_class]` attribute.
2323 the ` #[php_module] ` function.
2424
2525You 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
2727the same name as the field. Property types must implement ` IntoZval ` and
2828` FromZval ` .
2929
@@ -68,17 +68,16 @@ This example creates a PHP class `Human`, adding a PHP property `address`.
6868# #![cfg_attr(windows, feature(abi_vectorcall))]
6969# extern crate ext_php_rs;
7070# 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+ }
7780}
78- # #[php_module]
79- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
80- # module
81- # }
8281# fn main() {}
8382```
8483
@@ -89,22 +88,22 @@ it in the `Redis\Exception` namespace:
8988# #![cfg_attr(windows, feature(abi_vectorcall))]
9089# extern crate ext_php_rs;
9190use ext_php_rs::prelude::*;
92- use ext_php_rs::{exception::PhpException, zend::ce};
9391
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;
98100
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+ }
103106}
104- # #[php_module]
105- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
106- # module
107- # }
108107# fn main() {}
109108```
110109
@@ -116,47 +115,47 @@ The following example implements [`ArrayAccess`](https://www.php.net/manual/en/c
116115# #![cfg_attr(windows, feature(abi_vectorcall))]
117116# extern crate ext_php_rs;
118117use 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+ }
155158 }
156159}
157- # #[php_module]
158- # pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
159- # module
160- # }
161160# fn main() {}
162161```
0 commit comments