From de0f84fff5753f915f5ce1acf5166cb1e8a69269 Mon Sep 17 00:00:00 2001 From: ekinhbayar Date: Sun, 29 Nov 2020 18:59:16 +0300 Subject: [PATCH] Document named arguments Also documents deprecation of optional args after mandatory ones --- language/functions.xml | 128 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/language/functions.xml b/language/functions.xml index 6bc703776d01..e1915ba86f17 100644 --- a/language/functions.xml +++ b/language/functions.xml @@ -225,6 +225,29 @@ function takes_many_args( ]]> + + As of PHP 8.0.0, passing optional arguments after mandatory arguments + is deprecated. This can generally be resolved by dropping the default value. + One exception to this rule are parameters of the form + Type $param = &null;, where the &null; default makes the type implicitly + nullable. This usage remains allowed, though it is recommended to use an + explicit nullable type instead. + + + Passing optional arguments after mandatory arguments + + +]]> + + + Passing arguments by reference @@ -556,6 +579,111 @@ echo sum(1, 2, 3, 4); + + + Named Arguments + + + PHP 8.0.0 introduced named arguments as an extension of the existing + positional parameters. Named arguments allow passing arguments to a + function based on the parameter name, rather than the parameter position. + This makes the meaning of the argument self-documenting, makes the + arguments order-independent and allows skipping default values arbitrarily. + + + + Named arguments are passed by prefixing the value with the parameter name + followed by a colon. Using reserved keywords as parameter names is allowed. + The parameter name must be an identifier, specifying dynamically + is not allowed. + + + + Named argument syntax + + +]]> + + + + + Positional arguments versus named arguments + + +]]> + + + + + The order in which the named arguments are passed does not matter. + + + + Same as above example with a different order of parameters + + +]]> + + + + + You can combine named arguments with positional arguments. In this case, + the named arguments must come after the positional arguments. + It is also possible to specify only some of the optional arguments of a + function, regardless of their order. + + + + Combining named arguments with positional arguments + + +]]> + + + + + Passing the same parameter multiple times results in an Error exception. + + + + Error exception when passing the same parameter multiple times + + +]]> + + + +