@@ -8,15 +8,15 @@ return types.
88
99## Optional parameters
1010
11- Optional parameters can be used by setting the Rust parameter type to
12- ` Option<T> ` and then passing the name of the first optional parameter into the
13- macro options. Note that all parameters after the given parameter will be
14- optional as well, and therefore must be of the type ` Option<T> ` .
11+ Optional parameters can be used by setting the Rust parameter type to a variant
12+ of ` Option<T> ` . The macro will then figure out which parameters are optional by
13+ using the last consecutive arguments that are a variant of ` Option<T> ` or have a
14+ default value .
1515
1616``` rust
1717# extern crate ext_php_rs;
1818# use ext_php_rs :: prelude :: * ;
19- #[php_function(optional = " age " ) ]
19+ #[php_function]
2020pub fn greet (name : String , age : Option <i32 >) -> String {
2121 let mut greeting = format! (" Hello, {}!" , name );
2222
@@ -35,13 +35,59 @@ default, it does not need to be a variant of `Option`:
3535``` rust
3636# extern crate ext_php_rs;
3737# use ext_php_rs :: prelude :: * ;
38- #[php_function(optional = " offset " , defaults(offset = 0))]
38+ #[php_function(defaults(offset = 0))]
3939pub fn rusty_strpos (haystack : & str , needle : & str , offset : i64 ) -> Option <usize > {
4040 let haystack : String = haystack . chars (). skip (offset as usize ). collect ();
4141 haystack . find (needle )
4242}
4343```
4444
45+ Note that if there is a non-optional argument after an argument that is a
46+ variant of ` Option<T> ` , the ` Option<T> ` argument will be deemed a nullable
47+ argument rather than an optional argument.
48+
49+ ``` rust
50+ # extern crate ext_php_rs;
51+ # use ext_php_rs :: prelude :: * ;
52+ /// `age` will be deemed required and nullable rather than optional.
53+ #[php_function]
54+ pub fn greet (name : String , age : Option <i32 >, description : String ) -> String {
55+ let mut greeting = format! (" Hello, {}!" , name );
56+
57+ if let Some (age ) = age {
58+ greeting += & format! (" You are {} years old." , age );
59+ }
60+
61+ greeting += & format! (" {}." , description );
62+ greeting
63+ }
64+ ```
65+
66+ You can also specify the optional arguments if you want to have nullable
67+ arguments before optional arguments. This is done through an attribute
68+ parameter:
69+
70+ ``` rust
71+ # extern crate ext_php_rs;
72+ # use ext_php_rs :: prelude :: * ;
73+ /// `age` will be deemed required and nullable rather than optional,
74+ /// while description will be optional.
75+ #[php_function(optional = " description" )]
76+ pub fn greet (name : String , age : Option <i32 >, description : Option <String >) -> String {
77+ let mut greeting = format! (" Hello, {}!" , name );
78+
79+ if let Some (age ) = age {
80+ greeting += & format! (" You are {} years old." , age );
81+ }
82+
83+ if let Some (description ) = description {
84+ greeting += & format! (" {}." , description );
85+ }
86+
87+ greeting
88+ }
89+ ```
90+
4591## Throwing exceptions
4692
4793Exceptions can be thrown from inside a function which returns a ` Result<T, E> ` ,
0 commit comments