|
| 1 | +<?xml version="1.0" encoding="utf-8"?> |
| 2 | +<!-- EN-Revision: 2f1812217524ac60414745bad05cbbee00262b3b Maintainer: maiobarbero Status: ready --> |
| 3 | +<sect1 xml:id="language.operators.functional"> |
| 4 | + <title>Operatori Funzionali</title> |
| 5 | + <titleabbrev>Funzionali</titleabbrev> |
| 6 | + <para> |
| 7 | + PHP 8.5, e versioni successive, supportano un operatore che lavora direttamente sui callable. L'operatore |
| 8 | + <literal>|></literal>, o “pipe”, accetta un callable con singolo parametro a destra e gli passa |
| 9 | + il valore che si trova a sinistra, valutando infine il risultato del callable. Il callable |
| 10 | + a destra può essere un qualsiasi callable PHP valido: una <classname>Closure</classname>, |
| 11 | + un <link linkend="functions.first_class_callable_syntax">first-class callable</link>, |
| 12 | + un oggetto che implementa <link linkend="object.invoke">__invoke()</link>, ecc. |
| 13 | + </para> |
| 14 | + <para> |
| 15 | + Questo significa che le seguenti righe sono equivalenti. |
| 16 | + <example> |
| 17 | + <title>Uso di <literal>|></literal></title> |
| 18 | + <programlisting role="php"> |
| 19 | +<![CDATA[ |
| 20 | +<?php |
| 21 | +$result = "Hello World" |> strlen(...); |
| 22 | +echo $result, PHP_EOL; |
| 23 | +
|
| 24 | +$result = strlen("Hello World"); |
| 25 | +echo $result, PHP_EOL; |
| 26 | +?> |
| 27 | +]]> |
| 28 | + </programlisting> |
| 29 | + &example.outputs; |
| 30 | + <screen> |
| 31 | +<![CDATA[ |
| 32 | +11 |
| 33 | +11 |
| 34 | +]]> |
| 35 | + </screen> |
| 36 | + </example> |
| 37 | + </para> |
| 38 | + <para> |
| 39 | + Per una singola chiamata questo non è particolarmente utile. Diventa utile quando più chiamate sono concatenate insieme. |
| 40 | + Ciò significa che i seguenti frammenti di codice sono equivalenti dal punto di vista logico: |
| 41 | + <example> |
| 42 | + <title>Concatenare chiamate |></title> |
| 43 | + <programlisting role="php"> |
| 44 | +<![CDATA[ |
| 45 | +<?php |
| 46 | +$result = "PHP Rocks" |
| 47 | + |> htmlentities(...) |
| 48 | + |> str_split(...) |
| 49 | + |> (fn($x) => array_map(strtoupper(...), $x)) |
| 50 | + |> (fn($x) => array_filter($x, fn($v) => $v != 'O')) |
| 51 | +; |
| 52 | +print_r($result); |
| 53 | +
|
| 54 | +$temp = "PHP Rocks"; |
| 55 | +$temp = htmlentities($temp); |
| 56 | +$temp = str_split($temp); |
| 57 | +$temp = array_map(strtoupper(...), $temp); |
| 58 | +$temp = array_filter($temp, fn($v) => $v != 'O'); |
| 59 | +$result = $temp; |
| 60 | +print_r($result); |
| 61 | +?> |
| 62 | +]]> |
| 63 | + </programlisting> |
| 64 | + &example.outputs; |
| 65 | + <screen> |
| 66 | +<![CDATA[ |
| 67 | +Array |
| 68 | +( |
| 69 | + [0] => P |
| 70 | + [1] => H |
| 71 | + [2] => P |
| 72 | + [3] => |
| 73 | + [4] => R |
| 74 | + [6] => C |
| 75 | + [7] => K |
| 76 | + [8] => S |
| 77 | +) |
| 78 | +Array |
| 79 | +( |
| 80 | + [0] => P |
| 81 | + [1] => H |
| 82 | + [2] => P |
| 83 | + [3] => |
| 84 | + [4] => R |
| 85 | + [6] => C |
| 86 | + [7] => K |
| 87 | + [8] => S |
| 88 | +) |
| 89 | +]]> |
| 90 | + </screen> |
| 91 | + </example> |
| 92 | + </para> |
| 93 | + <para> |
| 94 | + La parte sinistra del pipe può essere qualsiasi valore o espressione. La parte destra |
| 95 | + può essere qualsiasi callable PHP valido che accetti un singolo parametro, o qualsiasi espressione |
| 96 | + che valuti a un callable di questo tipo. Le funzioni con più di un parametro richiesto |
| 97 | + non sono consentite e falliranno come se la funzione fosse chiamata normalmente |
| 98 | + con argomenti insufficienti. Le funzioni che accettano una variabile per riferimento non sono consentite. |
| 99 | + Se la parte destra non valuta in un callable valido, genererà un Error. |
| 100 | + </para> |
| 101 | + <note> |
| 102 | + <para> |
| 103 | + È importante notare che, per evitare ambiguità sintattiche, le <link linkend="functions.arrow">arrow function</link> |
| 104 | + DEVONO essere racchiuse tra parentesi quando vengono usate con un operatore pipe, come negli esempi precedenti. |
| 105 | + In caso contrario, si verificherà un errore fatale. |
| 106 | + </para> |
| 107 | + </note> |
| 108 | + |
| 109 | + <sect2 role="seealso"> |
| 110 | + &reftitle.seealso; |
| 111 | + <para> |
| 112 | + <simplelist> |
| 113 | + <member><classname>Closure</classname></member> |
| 114 | + </simplelist> |
| 115 | + </para> |
| 116 | + </sect2> |
| 117 | +</sect1> |
0 commit comments