reference/funchand/functions/call-user-func-array.xml
5832a97c699c5e1bc808ee900447fe751af0d540
...
...
@@ -11,11 +11,11 @@
11
11
<methodsynopsis>
12
12
<type>mixed</type><methodname>call_user_func_array</methodname>
13
13
<methodparam><type>callable</type><parameter>callback</parameter></methodparam>
14
-
<methodparam><type>array</type><parameter>param_arr</parameter></methodparam>
14
+
<methodparam><type>array</type><parameter>args</parameter></methodparam>
15
15
</methodsynopsis>
16
16
<para>
17
17
Calls the <parameter>callback</parameter> given by the first parameter with
18
-
the parameters in <parameter>param_arr</parameter>.
18
+
the parameters in <parameter>args</parameter>.
19
19
</para>
20
20
</refsect1>
21
21

...
...
@@ -32,10 +32,26 @@
32
32
</listitem>
33
33
</varlistentry>
34
34
<varlistentry>
35
-
<term><parameter>param_arr</parameter></term>
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>
...
...
@@ -63,12 +79,9 @@
63
79
</thead>
64
80
<tbody>
65
81
<row>
66
-
<entry>5.3.0</entry>
82
+
<entry>8.0.0</entry>
67
83
<entry>
68
-
The interpretation of object oriented keywords like <literal>parent</literal>
69
-
and <literal>self</literal> has changed. Previously, calling them using the
70
-
double colon syntax would emit an <constant>E_STRICT</constant> warning because
71
-
they were interpreted as static.
84
+
<parameter>args</parameter> keys will now be interpreted as parameter names, instead of being silently ignored.
72
85
</entry>
73
86
</row>
74
87
</tbody>
...
...
@@ -126,10 +139,8 @@ class Foo {
126
139
}
127
140
}
128
141

129
-
// As of PHP 5.3.0
130
142
call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));
131
143

132
-
// As of PHP 5.3.0
133
144
call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));
134
145

135
146
?>
...
...
@@ -153,7 +164,7 @@ $func = function($arg1, $arg2) {
153
164
return $arg1 * $arg2;
154
165
};
155
166

156
-
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)));
157
168

158
169
?>
159
170
]]>
...
...
@@ -190,29 +201,42 @@ global $bar=55
190
201
]]>
191
202
</screen>
192
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>
193
235
</para>
194
236
</refsect1>
195
237

196
238
<refsect1 role="notes">
197
239
&reftitle.notes;
198
-
<note>
199
-
<para>
200
-
Before PHP 5.4, referenced variables in <parameter>param_arr</parameter>
201
-
are passed to the function by reference, regardless of whether the function
202
-
expects the respective parameter to be passed by reference. This form of
203
-
call-time pass by reference does not emit a deprecation notice, but it is
204
-
nonetheless deprecated, and has been removed in PHP 5.4.
205
-
Furthermore, this does not apply to internal functions, for which
206
-
the function signature is honored. Passing by value when the function
207
-
expects a parameter by reference results in a warning and having
208
-
<function>call_user_func</function> return &false; (there is, however, an
209
-
exception for passed values with reference count = 1, such as in literals,
210
-
as these can be turned into references without ill effects — but also
211
-
without writes to that value having any effect —; do not rely
212
-
in this behavior, though, as the reference count is an implementation
213
-
detail and the soundness of this behavior is questionable).
214
-
</para>
215
-
</note>
216
240
&note.func-callback-exceptions;
217
241
</refsect1>
218
242

...
...
@@ -221,7 +245,6 @@ global $bar=55
221
245
<para>
222
246
<simplelist>
223
247
<member><function>call_user_func</function></member>
224
-
<member>&seealso.callback;</member>
225
248
<member><methodname>ReflectionFunction::invokeArgs</methodname></member>
226
249
<member><methodname>ReflectionMethod::invokeArgs</methodname></member>
227
250
</simplelist>
...
...
@@ -229,7 +252,6 @@ global $bar=55
229
252
</refsect1>
230
253

231
254
</refentry>
232
-

233
255
<!-- Keep this comment at the end of the file
234
256
Local variables:
235
257
mode: sgml
236
258