PHP 8.5 Adds Pipe Operator: What it means

20 lemper 8 8/5/2025, 4:13:43 AM thephp.foundation ↗

Comments (8)

gbalduzzi · 55s ago
I like it.

I really believe the thing PHP needs the most is a rework of string / array functions to make them more consistent and chain able. Now they are at least chain able.

I'm not a fan of the ... syntax though, especially when mixed in the same chain with the spread operator

sandreas · 11m ago
While I appreciate the effort and like the approach in general, in this use case I really would prefer extensions / extension functions (like in Kotlin[1]) or an IEnumerable / iterator approach (like in C#).

  $arr = [
    new Widget(tags: ['a', 'b', 'c']),
    new Widget(tags: ['c', 'd', 'e']),
    new Widget(tags: ['x', 'y', 'a']),
  ];
  
  $result = $arr
      |> fn($x) => array_column($x, 'tags') // Gets an array of arrays
      |> fn($x) => array_merge(...$x)       // Flatten into one big array
      |> array_unique(...)                  // Remove duplicates
      |> array_values(...)                  // Reindex the array.
  ;
feels much more complex than writing

  $result = $arr->column('tags')->flatten()->unique()->values()
having array extension methods for column, flatten, unique and values.

1: https://kotlinlang.org/docs/extensions.html#extension-functi...

librasteve · 4m ago
raku has had feed operators like this since its inception

  # pipeline functional style
  (1..5)
    ==> map { $_ * 2 }
    ==> grep { $_ > 5 }
    ==> say();              # (6 8 10)

  # method chain OO style
  (1..5)
    .map( * * 2)
    .grep( * > 5)
    .say                    # (6 8 10)
bapak · 21m ago
Meanwhile the JS world has been waiting for 10 years for this proposal, which is still in stage 2 https://github.com/tc39/proposal-pipeline-operator/issues/23...
wouldbecouldbe · 12m ago
It’s really not needed, syntax sugar. With dots you do almost the same. Php doesn’t have chaining. Adding more and more complexity doesn’t make a language better.
bapak · 5m ago
Nothing is really needed, C89 was good enough.

Dots are not the same, nobody wants to use chaining like underscore/lodash allowed because it makes dead code elimination impossible.

EGreg · 7m ago
It’s not really chaining

More like thenables / promises

wouldbecouldbe · 5m ago
It looks like chaining, but with possibility of adding custom functions?