Skip to content

Commit 7e4062a

Browse files
authored
docs(macro): improve name vs rename documentation
Refs: #422
1 parent 78d2499 commit 7e4062a

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

guide/src/macros/classes.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ well as registering the class to be registered with the `#[php_module]` macro.
66

77
## Options
88

9-
The attribute takes some options to modify the output of the class:
9+
There are additional macros that modify the class. These macros **must** be
10+
placed underneath the `#[php_class]` attribute.
1011

1112
- `name` - Changes the name of the class when exported to PHP. The Rust struct
1213
name is kept the same. If no name is given, the name of the struct is used.
1314
Useful for namespacing classes.
14-
15-
There are also additional macros that modify the class. These macros **must** be
16-
placed underneath the `#[php_class]` attribute.
17-
15+
- `rename` - Changes the case of the class name when exported to PHP.
1816
- `#[php(extends = ce)]` - Sets the parent class of the class. Can only be used once.
1917
`ce` must be a function with the signature `fn() -> &'static ClassEntry`.
2018
- `#[php(implements = ce)]` - Implements the given interface on the class. Can be used
@@ -97,7 +95,8 @@ use ext_php_rs::{
9795
zend::ce
9896
};
9997
100-
#[php_class(name = "Redis\\Exception\\RedisException")]
98+
#[php_class]
99+
#[php(name = "Redis\\Exception\\RedisException")]
101100
#[php(extends = ce::exception)]
102101
#[derive(Default)]
103102
pub struct RedisException;

guide/src/macros/impl.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ If you do not want a function exported to PHP, you should place it in a separate
1111

1212
If you want to use async Rust, use `#[php_async_impl]`, instead: see [here »](./async_impl.md) for more info.
1313

14+
## Options
15+
16+
By default all constants are renamed to UPPER_CASE and all methods are renamed to
17+
camelCase. This can be changed by passing the `rename_methods` and
18+
`rename_constants` as `#[php]` attributes on the `impl` block. The options are:
19+
20+
- `#[php(rename_methods = "snake_case")]` - Renames the method to snake case.
21+
- `#[php(rename_constants = "snake_case")]` - Renames the constant to snake case.
22+
23+
See the [`name` and `rename`](./php.md#name-and-rename) section for a list of all
24+
available cases.
25+
1426
## Methods
1527

1628
Methods basically follow the same rules as functions, so read about the
@@ -24,17 +36,6 @@ To access the underlying Zend object, you can take a reference to a
2436
`ZendClassObject<T>` in place of the self parameter, where the parameter must
2537
be named `self_`. This can also be used to return a reference to `$this`.
2638

27-
By default, all methods are renamed in PHP to the camel-case variant of the Rust
28-
method name. This can be changed on the `#[php_impl]` attribute, by passing one
29-
of the following as the `rename_methods` option:
30-
31-
- `"none"` - does not rename the methods.
32-
- `"camelCase"` - renames all methods to camel case (default).
33-
- `"snake_case"` - renames all methods to snake case.
34-
35-
For example, to disable renaming, change the `#[php_impl]` attribute to
36-
`#[php_impl(rename_methods = "none")]`.
37-
3839
The rest of the options are passed as separate attributes:
3940

4041
- `#[php(defaults(i = 5, b = "hello")]` - Sets the default value for parameter(s).

guide/src/macros/php.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,20 @@ Which attributes are available depends on the element you are annotating:
5151
| setter ||||||||
5252
| constructor ||||||||
5353
| abstract_method ||||||||
54+
55+
## `name` and `rename`
56+
57+
`name` and `rename` are mutually exclusive. The `name` attribute is used to set the name of
58+
an item to a string literal. The `rename` attribute is used to change the case of the name.
59+
60+
```rs
61+
#[php(name = "NEW_NAME")]
62+
#[php(rename = snake_case)]]
63+
```
64+
65+
Available cases are:
66+
- `snake_case`
67+
- `PascalCase`
68+
- `camelCase`
69+
- `UPPER_CASE`
70+
- `none` - No change

guide/src/migration-guides/v0.14.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,21 @@ the following variants are equivalent:
196196
```rs
197197
#[php(rename = case, vis = "public")]
198198
```
199+
200+
#### `name` vs `rename`
201+
202+
Previously the (re)name parameter was used to rename items. This has been
203+
unified to use `name` to set the name of an item to a string literal. The
204+
`rename` parameter is now used to change the case of the name.
205+
206+
```rs
207+
#[php(name = "NEW_NAME")]
208+
#[php(rename = snake_case)]]
209+
```
210+
211+
Available cases are:
212+
- `snake_case`
213+
- `PascalCase`
214+
- `camelCase`
215+
- `UPPER_CASE`
216+
- `none` - No change

0 commit comments

Comments
 (0)