-
Notifications
You must be signed in to change notification settings - Fork 895
Expand file tree
/
Copy pathcallable.xml
More file actions
265 lines (227 loc) · 6.02 KB
/
Copy pathcallable.xml
File metadata and controls
265 lines (227 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="language.types.callable">
<title>Callbacks / Callables</title>
<para>
Callbacks can be denoted by the <type>callable</type> type declaration.
</para>
<para>
Some functions like <function>call_user_func</function> or
<function>usort</function> accept user-defined callback functions as a
parameter. Callback functions can not only be simple functions, but also
<type>object</type> methods, including static class methods.
</para>
<sect2 xml:id="language.types.callable.passing">
<title>Passing</title>
<para>
A PHP function is passed by its name as a <type>string</type>. Any built-in
or user-defined function can be used, except language constructs such as:
<function>array</function>, <function>echo</function>,
<function>empty</function>, <function>eval</function>,
<function>exit</function>, <function>isset</function>,
<function>list</function>, <function>print</function> or
<function>unset</function>.
</para>
<para>
A method of an instantiated <type>object</type> is passed as an
<type>array</type> containing an <type>object</type> at index 0 and the
method name at index 1. Passing protected and private methods
with the <type>callable</type> syntax is allowed.
</para>
<note>
<para>
If a callable of a protected or a private method is invoked from outside the class
with no visibility of said methods (e.g. being called from a POSIX signal handler),
a runtime error is thrown because the callable is invalid at the time of invocation.
</para>
</note>
<para>
Static class methods can also be passed without instantiating an
<type>object</type> of that class by either, passing the class name
instead of an <type>object</type> at index 0, or passing
<literal>'ClassName::methodName'</literal>.
</para>
<para>
Apart from common user-defined function,
<link linkend="functions.anonymous">anonymous functions</link> and
<link linkend="functions.arrow">arrow functions</link> can also be
passed to a callback parameter.
</para>
<note>
<para>
As of PHP 8.1.0, anonymous functions can also be created using the <link linkend="functions.first_class_callable_syntax">first class callable syntax</link>.
</para>
</note>
<para>
Generally, any object implementing <link linkend="object.invoke">__invoke()</link> can also
be passed to a callback parameter.
</para>
<para>
<example>
<title>
Callback function examples
</title>
<programlisting role="php">
<![CDATA[
<?php
// An example callback function
function my_callback_function() {
echo 'hello world!';
}
// An example callback method
class MyClass {
static function myCallbackMethod() {
echo 'Hello World!';
}
}
// Type 1: Simple callback
call_user_func('my_callback_function');
// Type 2: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod'));
// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');
// Type 5: Relative static class method call
class A {
public static function who() {
echo "A\n";
}
}
class B extends A {
public static function who() {
echo "B\n";
}
}
call_user_func(array('B', 'parent::who')); // A, deprecated as of PHP 8.2.0
// Type 6: Objects implementing __invoke can be used as callables
class C {
public function __invoke($name) {
echo 'Hello ', $name, "\n";
}
}
$c = new C();
call_user_func($c, 'PHP!');
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>
Callback example using a Closure
</title>
<programlisting role="php">
<![CDATA[
<?php
// Our closure
$double = function($a) {
return $a * 2;
};
// This is our range of numbers
$numbers = range(1, 5);
// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers = array_map($double, $numbers);
print implode(' ', $new_numbers);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
2 4 6 8 10
]]>
</screen>
</example>
</para>
<para>
<example>
<title>
Manually invoking callbacks
</title>
<programlisting role="php">
<![CDATA[
<?php
// define a callback handler that executes the callable (no parameters)
function invokeCallback(callable $theCallback) {
$theCallback();
}
// prints "42"
invokeCallback(function () {
echo "42" . PHP_EOL;
});
// define a callback handler that executes the callable with a parameter
function processNumber(callable $processor, int $theNumber) {
// process a number, and print its result
$result = $processor($theNumber);
echo $result . PHP_EOL;
}
// prints "72"
processNumber(fn ($x) => $x * 3, 24);
// define a class implementing the __invoke method
class NumberTripler
{
public function __invoke($x)
{
return $x * 3;
}
}
// prints "15"
$tripler = new NumberTripler();
processNumber($tripler, 5);
// define a class with a private instance method, to be called with the callable syntax
class HiddenFactory
{
private function hiddenWork()
{
echo "256" . PHP_EOL;
}
public function getProof()
{
return Closure::fromCallable([$this, 'hiddenWork']);
}
}
// prints "256"
$factory = new HiddenFactory();
$theProof = $factory->getProof();
invokeCallback($theProof);
?>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
42
72
15
256
]]>
</screen>
</example>
</para>
¬e.func-callback-exceptions;
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->