reference/funchand/functions/call-user-func-array.xml
5832a97c699c5e1bc808ee900447fe751af0d540
...
...
@@ -35,7 +35,23 @@
35
35
<term><parameter>args</parameter></term>
36
36
<listitem>
37
37
<para>
38
-
The parameters to be passed to the callback, as an indexed array.
38
+
The parameters to be passed to the callback, as an array.
39
+
</para>
40
+
<para>
41
+
If the keys of <parameter>args</parameter> are all numeric,
42
+
the keys are ignored and each element will be passed to
43
+
<parameter>callback</parameter> as a positional argument, in
44
+
order.
45
+
</para>
46
+
<para>
47
+
If any keys of <parameter>args</parameter> are strings,
48
+
those elements will be passed to <parameter>callback</parameter>
49
+
as named arguments, with the name given by the key.
50
+
</para>
51
+
<para>
52
+
It is a fatal error to have a numeric key in <parameter>args</parameter>
53
+
appear after a string key, or to have a string key that does not
54
+
match the name of any parameter of <parameter>callback</parameter>.
39
55
</para>
40
56
</listitem>
41
57
</varlistentry>
...
...
@@ -50,6 +66,30 @@
50
66
</para>
51
67
</refsect1>
52
68

69
+
<refsect1 role="changelog">
70
+
&reftitle.changelog;
71
+
<para>
72
+
<informaltable>
73
+
<tgroup cols="2">
74
+
<thead>
75
+
<row>
76
+
<entry>&Version;</entry>
77
+
<entry>&Description;</entry>
78
+
</row>
79
+
</thead>
80
+
<tbody>
81
+
<row>
82
+
<entry>8.0.0</entry>
83
+
<entry>
84
+
<parameter>args</parameter> keys will now be interpreted as parameter names, instead of being silently ignored.
85
+
</entry>
86
+
</row>
87
+
</tbody>
88
+
</tgroup>
89
+
</informaltable>
90
+
</para>
91
+
</refsect1>
92
+

53
93
<refsect1 role="examples">
54
94
&reftitle.examples;
55
95
<para>
...
...
@@ -99,10 +139,8 @@ class Foo {
99
139
}
100
140
}
101
141

102
-
// As of PHP 5.3.0
103
142
call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));
104
143

105
-
// As of PHP 5.3.0
106
144
call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));
107
145

108
146
?>
...
...
@@ -126,7 +164,7 @@ $func = function($arg1, $arg2) {
126
164
return $arg1 * $arg2;
127
165
};
128
166

129
-
var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */
167
+
var_dump(call_user_func_array($func, array(2, 4)));
130
168

131
169
?>
132
170
]]>
...
...
@@ -163,29 +201,42 @@ global $bar=55
163
201
]]>
164
202
</screen>
165
203
</example>
204
+
<example>
205
+
<title><function>call_user_func_array</function> using named arguments</title>
206
+
<programlisting role="php">
207
+
<![CDATA[
208
+
<?php
209
+
function foobar($first, $second) {
210
+
echo __FUNCTION__, " got $first and $second\n";
211
+
}
212
+

213
+
// Call the foobar() function with named arguments in non-positional order
214
+
call_user_func_array("foobar", array("second" => "two", "first" => "one"));
215
+

216
+
// Call the foobar() function with one named argument
217
+
call_user_func_array("foobar", array("foo", "second" => "bar"));
218
+

219
+
// Fatal error: Cannot use positional argument after named argument
220
+
call_user_func_array("foobar", array("first" => "one", "bar"));
221
+

222
+
?>
223
+
]]>
224
+
</programlisting>
225
+
&example.outputs.similar;
226
+
<screen>
227
+
<![CDATA[
228
+
foobar got one and two
229
+
foobar got foo and bar
230
+

231
+
Fatal error: Uncaught Error: Cannot use positional argument after named argument
232
+
]]>
233
+
</screen>
234
+
</example>
166
235
</para>
167
236
</refsect1>
168
237

169
238
<refsect1 role="notes">
170
239
&reftitle.notes;
171
-
<note>
172
-
<para>
173
-
Before PHP 5.4, referenced variables in <parameter>args</parameter>
174
-
are passed to the function by reference, regardless of whether the function
175
-
expects the respective parameter to be passed by reference. This form of
176
-
call-time pass by reference does not emit a deprecation notice, but it is
177
-
nonetheless deprecated, and has been removed in PHP 5.4.
178
-
Furthermore, this does not apply to internal functions, for which
179
-
the function signature is honored. Passing by value when the function
180
-
expects a parameter by reference results in a warning and having
181
-
<function>call_user_func</function> return &false; (there is, however, an
182
-
exception for passed values with reference count = 1, such as in literals,
183
-
as these can be turned into references without ill effects — but also
184
-
without writes to that value having any effect —; do not rely
185
-
in this behavior, though, as the reference count is an implementation
186
-
detail and the soundness of this behavior is questionable).
187
-
</para>
188
-
</note>
189
240
&note.func-callback-exceptions;
190
241
</refsect1>
191
242

192
243