-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathcallable.xml
More file actions
312 lines (270 loc) · 9.12 KB
/
callable.xml
File metadata and controls
312 lines (270 loc) · 9.12 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 312c0c7b39d0722c419f6784cbda24823220dfb3 Maintainer: mumumu Status: ready -->
<sect1 xml:id="language.types.callable">
<title>コールバック / Callable</title>
<simpara>
callable は、
別の関数への引数として渡される関数またはメソッドを参照するものです。
これらは、<type>callable</type> 型を宣言することで表されます。
</simpara>
<informalexample>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
function foo(callable $callback) {
$callback();
}
?>
]]>
</programlisting>
</informalexample>
<simpara>
関数によっては、コールバック関数をパラメータとして受け入れるものがあります。
例として
<function>array_map</function>, <function>usort</function>,
<function>preg_replace_callback</function> が挙げられます。
</simpara>
<sect2 xml:id="language.types.callable.passing">
<title>callable の作成</title>
<simpara>
callable は、呼び出し可能な何かを表す型です。
コールバック関数をパラメータとして期待する関数や、
メソッドの引数として渡すことができますし、
直接呼び出すこともできます。
<type>callable</type> 型は、
クラスのプロパティの型宣言には使えません。
代わりに、<classname>Closure</classname> を型宣言に使ってください。
</simpara>
<simpara>
callable は、複数の異なる方法で作成できます:
</simpara>
<itemizedlist>
<listitem>
<simpara><classname>Closure</classname> オブジェクト</simpara>
</listitem>
<listitem>
<simpara>関数またはメソッドの名前を含む文字列</simpara>
</listitem>
<listitem>
<simpara>
配列。クラス名または <type>object</type>
をインデックス 0 に、メソッド名をインデックス 1 に含みます。
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="object.invoke">__invoke()</link>
マジックメソッドを実装した &object;
</simpara>
</listitem>
</itemizedlist>
<simpara>
<classname>Closure</classname> オブジェクトは、
<link linkend="functions.anonymous">無名関数</link>、
<link linkend="functions.arrow">アロー関数</link>、
<link linkend="functions.first_class_callable_syntax">第一級callableを生成する記法</link>、
もしくは <methodname>Closure::fromCallable</methodname>
メソッドを使って作成できます。
</simpara>
<note>
<simpara>
<link linkend="functions.first_class_callable_syntax">第一級callableを生成する記法</link>
は、PHP 8.1.0 以降でのみ利用可能です。
</simpara>
</note>
<example>
<title>
<classname>Closure</classname> を使ったコールバックの例
</title>
<programlisting role="php">
<![CDATA[
<?php
// Using anonymous function syntax
$double1 = function ($a) {
return $a * 2;
};
// Using first-class callable syntax
function double_function($a) {
return $a * 2;
}
$double2 = double_function(...);
// Using arrow function syntax
$double3 = fn($a) => $a * 2;
// Using Closure::fromCallable
$double4 = Closure::fromCallable('double_function');
// Use the closure as a callback here to
// double the size of each element in our range
$new_numbers = array_map($double1, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double2, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double3, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double4, range(1, 5));
print implode(' ', $new_numbers);
?>
]]>
</programlisting>
&example.outputs.81;
<screen>
<![CDATA[
2 4 6 8 10
2 4 6 8 10
2 4 6 8 10
2 4 6 8 10
]]>
</screen>
</example>
<simpara>
callable は、
関数名や static メソッドを含む文字列でも表現できます。
ビルトイン関数、またはユーザー定義関数も使えます。
但し、以下のような言語構造は除きます:
<function>array</function>, <function>echo</function>,
<function>empty</function>, <function>eval</function>,
<function>isset</function>,
<function>list</function>, <function>print</function>,
<function>unset</function>
</simpara>
<simpara>
static メソッドは、クラスの <type>object</type>
型をインスタンス化せずに使えます。
クラス名をインデックス 0 に、
メソッド名をインデックス 1 に配置した配列を作成するか、
<literal>'ClassName::methodName'</literal> のような、
スコープ解決演算子 <literal>::</literal> を使った特殊構文が使えます。
</simpara>
<simpara>
インスタンス化された <type>object</type>
のメソッドは、<type>object</type> をインデックス 0 に、
メソッド名をインデックス 1 に配置した配列を作成することで、
callable にできます。
</simpara>
<simpara>
<classname>Closure</classname> オブジェクトと callable 型の主な違いは、
<classname>Closure</classname>
オブジェクトがスコープに依存せず常に呼び出せるのに対し、
callable はスコープに依存する場合があり、
直接呼び出せない可能性がある点です。
<classname>Closure</classname> が、callable を作成する望ましい方法です。
</simpara>
<note>
<simpara>
<classname>Closure</classname> オブジェクトは、
それが作成されたスコープにバインドされますが、
クラスメソッドを文字列または配列で参照する callable は、
それらが呼び出されたスコープの内部で解決されます。
callable を private または protected メソッドから作成し、
クラスのスコープ外から呼び出せるようにするには、
<methodname>Closure::fromCallable</methodname> または、
<link linkend="functions.first_class_callable_syntax">第一級callableを生成する記法</link> を使います。
</simpara>
</note>
<simpara>
PHP は、コールバック引数として使えるものの、
直接は呼び出せない callable を作成できます。
これらはコンテキスト依存の callable であり、
例えば <literal>'parent::method'</literal> や
<literal>["static", "method"]</literal>
の形で、クラスの継承階層におけるクラスメソッドを参照します。
</simpara>
<note>
<simpara>
PHP 8.2.0 以降では、
コンテキスト依存の callable は推奨されなくなりました。
<literal>'parent::method'</literal> を
<literal>parent::class . '::method'</literal>
形式に置き換えることでコンテキスト依存を除去するか、
<link linkend="functions.first_class_callable_syntax">第一級callableを生成する記法</link> を使ってください。
</simpara>
</note>
<example>
<title>
<function>call_user_function</function>
を使って、様々なタイプの callable を呼び出す
</title>
<programlisting role="php">
<![CDATA[
<?php
// An example callback function
function my_callback_function() {
echo 'hello world!', PHP_EOL;
}
// An example callback method
class MyClass {
static function myCallbackMethod() {
echo 'Hello World!', PHP_EOL;
}
}
// Type 1: Simple callback
call_user_func('my_callback_function');
// Type 2: Static class method call
call_user_func(['MyClass', 'myCallbackMethod']);
// Type 3: Object method call
$obj = new MyClass();
call_user_func([$obj, 'myCallbackMethod']);
// Type 4: Static class method call
call_user_func('MyClass::myCallbackMethod');
// Type 5: Static class method call using ::class keyword
call_user_func([MyClass::class, 'myCallbackMethod']);
// Type 6: Relative static class method call
class A {
public static function who() {
echo 'A', PHP_EOL;
}
}
class B extends A {
public static function who() {
echo 'B', PHP_EOL;
}
}
call_user_func(['B', 'parent::who']); // deprecated as of PHP 8.2.0
// Type 7: Objects implementing __invoke can be used as callables
class C {
public function __invoke($name) {
echo 'Hello ', $name;
}
}
$c = new C();
call_user_func($c, 'PHP!');
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
hello world!
Hello World!
Hello World!
Hello World!
Hello World!
Deprecated: Callables of the form ["B", "parent::who"] are deprecated in script on line 41
A
Hello PHP!
]]>
</screen>
</example>
¬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
-->