PHP 8.5, due out November of this year, will bring with it another long-sought-after feature: the pipe operator (|>). It’s a small feature with huge potential, yet it still took years to happen. By Larry Garfield.

On its own, that is not all that interesting. Where it becomes interesting is when it is repeated, or chained, to form a “pipeline.” For example, here’s real code example:

$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.
;

// $result is ['a', 'b', 'c', 'd', 'e', 'x', 'y']

The article further explains:

  • What is a pipe operator?
  • Where did it come from?
  • More than the sum of its parts
  • What comes next?

The first is a second attempt at Partial Function Application. This is a larger feature, but with first-class callables already bringing in much of the necessary plumbing, which simplifies the implementation. With pipes now providing a natural use case, as well as easy optimization points, it’s worth a second attempt. Whether it makes it into PHP 8.5, is delayed to 8.6, or is again rejected is still an open question as of this writing, though I am hopeful. Major thanks to Arnaud Le Blanc from the PHP Foundation team for picking it up to update the implementation. Interesting read!

[Read More]

Tags php cloud software-architecture app-development web-development