svn: /phpdoc/en/trunk/language/ functions.xml

From: Date: Sun, 01 Jun 2014 22:36:06 +0000
Subject: svn: /phpdoc/en/trunk/language/ functions.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-12513@lists.php.net to get a copy of this message
aharvey Sun, 01 Jun 2014 22:36:06 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=333678 Log: Add ... documentation. Changed paths: U phpdoc/en/trunk/language/functions.xml Modified: phpdoc/en/trunk/language/functions.xml =================================================================== --- phpdoc/en/trunk/language/functions.xml 2014-06-01 22:08:56 UTC (rev 333677) +++ phpdoc/en/trunk/language/functions.xml 2014-06-01 22:36:06 UTC (rev 333678) @@ -178,10 +178,7 @@ reference</link>, and <link linkend="functions.arguments.default">default argument values</link>. <link linkend="functions.variable-arg-list">Variable-length - argument lists</link> are also supported, see also the function references for - <function>func_num_args</function>, - <function>func_get_arg</function>, and - <function>func_get_args</function> for more information. + argument lists</link> are also supported. </para> <para> <example> @@ -362,18 +359,173 @@ <simpara> PHP has support for variable-length argument lists in - user-defined functions. This is really quite easy, using the + user-defined functions. This is implemented using the + <literal>...</literal> token in PHP 5.6 and later, and using the <function>func_num_args</function>, <function>func_get_arg</function>, and - <function>func_get_args</function> functions. + <function>func_get_args</function> functions in PHP 5.5 and earlier. </simpara> - <simpara> - No special syntax is required, and argument lists may still be - explicitly provided with function definitions and will behave as - normal. - </simpara> + <sect3 xml:id="functions.variable-arg-list.new"> + <title><literal>...</literal> in PHP 5.6+</title> + <para> + In PHP 5.6 and later, argument lists may include the + <literal>...</literal> token to denote that the function accepts a + variable number of arguments. The arguments will be passed into the + given variable as an array; for example: + + <example> + <title>Using <literal>...</literal> to access variable arguments</title> + <programlisting role="php"> +<![CDATA[ +<?php +function sum(...$numbers) { + $acc = 0; + foreach ($numbers as $n) { + $acc += $n; + } + return $acc; +} + +echo sum(1, 2, 3, 4); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +10 +]]> + </screen> + </example> + </para> + + <para> + You can also use <literal>...</literal> when calling functions to unpack + an <type>array</type> or <classname>Traversable</classname> variable or + literal into the argument list: + + <example> + <title>Using <literal>...</literal> to provide arguments</title> + <programlisting role="php"> +<![CDATA[ +<?php +function add($a, $b) { + return $a + $b; +} + +echo add(...[1, 2])."\n"; + +$a = [1, 2]; +echo add(...$a); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +3 +3 +]]> + </screen> + </example> + </para> + + <para> + You may specify normal positional arguments before the + <literal>...</literal> token. In this case, only the trailing arguments + that don't match a positional argument will be added to the array + generated by <literal>...</literal>. + </para> + + <para> + It is also possible to add a + <link linkend="language.oop5.typehinting">type hint</link> before the + <literal>...</literal> token. If this is present, then all arguments + captured by <literal>...</literal> must be objects of the hinted class. + + <example> + <title>Type hinted variable arguments</title> + <programlisting role="php"> +<![CDATA[ +<?php +function total_intervals($unit, DateInterval ...$intervals) { + $time = 0; + foreach ($intervals as $interval) { + $time += $interval->$unit; + } + return $time; +} + +$a = new DateInterval('P1D'); +$b = new DateInterval('P2D'); +echo total_intervals('d', $a, $b).' days'; + +// This will fail, since null isn't a DateInterval object. +echo total_intervals('d', null); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +3 days +Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2 +]]> + </screen> + </example> + </para> + + <para> + Finally, you may also pass variable arguments + <link linkend="functions.arguments.by-reference">by reference</link> by + prefixing the <literal>...</literal> with an ampersand + (<literal>&amp;</literal>). + </para> + </sect3> + + <sect3 xml:id="functions.variable-arg-list.old"> + <title>Older versions of PHP</title> + + <para> + No special syntax is required to note that a function is variadic; + however access to the function's arguments must use + <function>func_num_args</function>, <function>func_get_arg</function> + and <function>func_get_args</function>. + </para> + + <para> + The first example above would be implemented as follows in PHP 5.5 and + earlier: + + <example> + <title>Accessing variable arguments in PHP 5.5 and earlier</title> + <programlisting role="php"> +<![CDATA[ +<?php +function sum() { + $acc = 0; + foreach (func_get_args() as $n) { + $acc += $n; + } + return $acc; +} + +echo sum(1, 2, 3, 4); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +10 +]]> + </screen> + </example> + </para> + </sect3> + </sect2> </sect1>

« previous php.doc.cvs (#12513) next »