Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP8.5から導入されたパイプ演算子

 PHP8.5から導入されたパイプ演算子

えびてく #7 で発表したライトニングトークのスライドです。
https://ebitech.connpass.com/event/393193/

More Decks by 上垣雅弘 / UEGAKI Masahiro

Other Decks in Programming

Transcript

  1. 自己紹介 名前 : 上垣 雅弘 ハンドルネーム : 睦月 Xアカウント :

    @ichigatu 札幌PHP勉強会の運営をしています。 猫の動画を見るのが好きです。 最近、モータルコンバット/ネクストラウンドを 映画館で観ました。
  2. PHP 8.5 以降では、callable に直接値を渡す演算子を サポートしています。 |> 演算子、または "パイプ" は、 右辺にパラメーターをひとつ取る

    callable を受け入れ、 左辺値をそれに渡し、callable の結果を評価します。 右 辺の callable は、有効な PHP の callable であれば何 でも構いません: つまり、Closure、 第一級callableを 生成する記法、 __invoke() を実装したオブジェクトな どです。 パイプ演算子とは?
  3. <?php $result = "Hello World" |> strlen(...); echo $result, PHP_EOL;

    $result = strlen("Hello World"); echo $result, PHP_EOL; ?> 例1 |> を使う
  4. <?php $temp = "PHP Rocks"; $temp = htmlentities($temp); $temp =

    str_split($temp); $temp = array_map(strtoupper(...), $temp); $temp = array_filter($temp, fn($v) => $v != 'O'); $result = $temp; print_r($result); ?> 例2 |> の呼び出しをチェインさせる パイプ演算子を使わない場合
  5. Array ( [0] => P [1] => H [2] =>

    P [3] => [4] => R [6] => C [7] => K [8] => S ) 実行結果
  6. <?php $result = "PHP Rocks" |> htmlentities(...) |> str_split(...) |>

    (fn($x) => array_map(strtoupper(...), $x)) |> (fn($x) => array_filter($x, fn($v) => $v != 'O')) ; print_r($result); ?> パイプ演算子を使った場合