@@ -18,7 +18,7 @@ pub fn get_module() -> Module {
1818 let name = arguments[0].expect_z_str()?.to_str()?;
1919 echo!("Hello, {}!\n", name);
2020 Ok(())
21- }).argument(Argument::by_val ("name"));
21+ }).argument(Argument::new ("name"));
2222
2323 module
2424}
@@ -71,7 +71,7 @@ pub fn get_module() -> Module {
7171 let count = arguments[0].expect_mut_z_ref()?;
7272 *count.val_mut().expect_mut_long()? += 100;
7373 Ok(())
74- }).argument(Argument::by_ref ("count"));
74+ }).argument(Argument::new ("count").by_ref( ));
7575
7676 module
7777}
@@ -80,3 +80,44 @@ pub fn get_module() -> Module {
8080Here, the argument is registered as
8181[ ` Argument::by_ref ` ] ( phper::functions::Argument::by_ref ) . Therefore, the type of
8282the ` count ` parameter is no longer long, but a reference.
83+
84+ ## Argument and return type modifiers
85+
86+ Arguments can have type-hints, nullability and default values applied. Here we define a function that accepts
87+ a nullable class (in this case, an interface), and a string with a default value:
88+
89+ ``` rust,no_run
90+ use phper::{modules::Module, php_get_module, functions::Argument, echo};
91+ use phper::types::ArgumentTypeHint;
92+
93+ #[php_get_module]
94+ pub fn get_module() -> Module {
95+ let mut module = Module::new(
96+ env!("CARGO_CRATE_NAME"),
97+ env!("CARGO_PKG_VERSION"),
98+ env!("CARGO_PKG_AUTHORS"),
99+ );
100+
101+ module.add_function("my_function", |_| -> phper::Result<()> {
102+ Ok(())
103+ })
104+ .argument(Argument::new("a_class").with_type_hint(ArgumentTypeHint::ClassEntry(String::from(r"\MyNamespace\MyInterface"))).allow_null())
105+ .argument(Argument::new("name").with_type_hint(ArgumentTypeHint::String).with_default_value("'my_default'"))
106+ .argument(Argument::new("optional_bool").with_type_hint(ArgumentTypeHint::Bool).optional());
107+
108+ module
109+ }
110+ ```
111+
112+ The output of ` php --re ` for this function would look like:
113+
114+ ``` txt
115+ Function [ <internal:integration> function my_function ] {
116+
117+ - Parameters [3] {
118+ Parameter #0 [ <required> ?class_name $a_class ]
119+ Parameter #1 [ <optional> string $name = 'my_default' ]
120+ Parameter #2 [ <optional> bool $optional_bool = <default> ]
121+ }
122+ }
123+ ```
0 commit comments