Skip to content

Commit 4af0280

Browse files
committed
Document the pipe operator.
php/doc-en@2946c8a
1 parent 9844e93 commit 4af0280

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

language/operators.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- $Revision$ -->
3-
<!-- EN-Revision: 16934048f79c6e117cd16a23c09c1b2ea502e284 Maintainer: takagi Status: ready -->
3+
<!-- EN-Revision: 2946c8a267734a9e8696e1764f7436e6caa8909c Maintainer: takagi Status: ready -->
44
<!-- CREDITS: hirokawa,mumumu -->
55
<chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook" annotations="interactive">
66
<title>演算子</title>
@@ -42,6 +42,7 @@
4242
&language.operators.string;
4343
&language.operators.array;
4444
&language.operators.type;
45+
&language.operators.functional;
4546

4647
</chapter>
4748
<!-- Keep this comment at the end of the file

language/operators/functional.xml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- $Revision$ -->
3+
<!-- EN-Revision: 2946c8a267734a9e8696e1764f7436e6caa8909c Maintainer: mumumu Status: ready -->
4+
<sect1 xml:id="language.operators.functional">
5+
<title>パイプ演算子</title>
6+
<titleabbrev>パイプ演算子</titleabbrev>
7+
<para>
8+
PHP 8.5 以降では、callable に直接値を渡す演算子をサポートしています。
9+
<literal>|></literal> 演算子、または "パイプ" は、右辺にパラメーターをひとつ取る
10+
callable を受け入れ、左辺値をそれに渡し、callable の結果を評価します。
11+
右辺の callable は、有効な PHP の callable であれば何でも構いません:
12+
つまり、<classname>Closure</classname>、
13+
<link linkend="functions.first_class_callable_syntax">第一級callableを生成する記法</link>、
14+
<link linkend="object.invoke">__invoke()</link> を実装したオブジェクトなどです。
15+
</para>
16+
<para>
17+
論理的には同じ意味になる、2行のコードを以下に示します。
18+
<example>
19+
<title><literal>|></literal> を使う</title>
20+
<programlisting role="php">
21+
<![CDATA[
22+
<?php
23+
$result = "Hello World" |> strlen(...);
24+
print $result . PHP_EOL;
25+
26+
$result = strlen("Hello World");
27+
print $result . PHP_EOL;
28+
?>
29+
]]>
30+
</programlisting>
31+
&example.outputs;
32+
<screen>
33+
<![CDATA[
34+
11
35+
11
36+
]]>
37+
</screen>
38+
</example>
39+
</para>
40+
<para>
41+
単一の呼び出しは特に役に立ちませんが、
42+
複数の呼び出しを一緒にチェインさせると役に立ちます。
43+
たとえば、論理的には同じ意味になるコード片を2つ、以下に示します:
44+
<example>
45+
<title>|> の呼び出しをチェインさせる</title>
46+
<programlisting role="php">
47+
<![CDATA[
48+
<?php
49+
$result = "PHP Rocks"
50+
|> htmlentities(...)
51+
|> str_split(...)
52+
|> (fn($x) => array_map(strtoupper(...), $x))
53+
|> (fn($x) => array_filter($x, fn($v) => $v != 'O'))
54+
;
55+
print $result . PHP_EOL;
56+
57+
$temp = "PHP Rocks";
58+
$temp = htmlentities($temp);
59+
$temp = str_split($temp);
60+
$temp = array_map(strtoupper(...), $temp);
61+
$temp = array_filter($temp, fn($v) => $v != 'O');
62+
$result = $temp;
63+
print $result . PHP_EOL;
64+
?>
65+
]]>
66+
</programlisting>
67+
&example.outputs;
68+
<screen>
69+
<![CDATA[
70+
Array
71+
(
72+
[0] => P
73+
[1] => H
74+
[2] => P
75+
[3] =>
76+
[4] => R
77+
[6] => C
78+
[7] => K
79+
[8] => S
80+
)
81+
Array
82+
(
83+
[0] => P
84+
[1] => H
85+
[2] => P
86+
[3] =>
87+
[4] => R
88+
[6] => C
89+
[7] => K
90+
[8] => S
91+
)
92+
]]>
93+
</screen>
94+
</example>
95+
</para>
96+
<para>
97+
パイプの左側は、値や式であれば何でも構いません。
98+
パイプの右側は、パラメーターをひとつ取る PHP の有効な callable か、
99+
そのように評価できる任意の式であれば何でも構いません。
100+
必須のパラメーターをひとつ以上とる関数を右側に指定した場合、
101+
それは許可されず、
102+
引数が不足した状態で関数をコールしたかのように失敗します。
103+
リファレンスの値を取る関数も許可されていません。
104+
右辺が有効な callable として評価されない場合、Error がスローされます。
105+
</para>
106+
<note>
107+
<para>
108+
上の例で示したとおり、文法的な曖昧さを回避するため、
109+
<link linkend="functions.arrow">アロー関数</link> は、
110+
パイプ演算子を一緒に使う場合に括弧で囲わなければならない点に注意してください。
111+
そうしない場合、致命的なエラーが発生します。
112+
</para>
113+
</note>
114+
115+
<sect2 role="seealso">
116+
&reftitle.seealso;
117+
<para>
118+
<simplelist>
119+
<member><classname>Closure</classname></member>
120+
</simplelist>
121+
</para>
122+
</sect2>
123+
</sect1>

0 commit comments

Comments
 (0)