Closed6

PHP 8.5.0 beta1 の Pipe Operator を試す

uuauua

docker compose で debian を立てる

compose.yml

services:
  debian:
    image: debian:bullseye
    tty: true
    command: sleep infinity

立ち上げ

docker compose up

コンテナに入って、コマンドを実行。

apt update
apt install -y git

# see https://github.com/php/php-src
apt install -y pkg-config build-essential autoconf bison re2c libxml2-dev libsqlite3-dev

# see https://php.watch/versions/8.5/releases/8.5.0beta1
git clone https://github.com/php/php-src.git --depth 1 --branch php-8.5.0beta1
cd php-src

# ビルド
./buildconf --force 
./configure
make install

ビルド方法は php-src の README に詳しく書いてあった。
https://github.com/php/php-src

uuauua

PHP 8.5.0 beta1 のビルド完了

$ php -v
PHP 8.5.0beta1 (cli) (built: Aug 15 2025 11:29:48) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.0beta1, Copyright (c), by Zend Technologies
uuauua

本題。
Pipeline Operator を試す。

<?php

$list = ['a', 'b', 'c', 'd', 'a', 'e'];

$ret = $list
        |> array_unique(...)
        |> array_values(...);
var_dump($ret);

これを実行すると

$ php main.php
array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
}

こんな感じか。

uuauua

引数が複数あるときは、、

<?php

$list = ['a', 'b', 'c', 'd', 'a', 'e'];

$ret = $list
        |> fn(array $a) => array_filter($a, fn(string $x) => $x === 'd');
var_dump($ret);

これを実行すると

$ php main.php
array(1) {
  [3]=>
  string(1) "d"
}
このスクラップは28日前にクローズされました