Breaking News
Loading latest updates...
Loading latest updates...
Markets
Loading market data...
Loading market data...
Loading market data...
Loading market data...
Loading market data...
Piper Brings Laravel-Style Array and String Helpers to PHP 8.5 Pipe Operator
Technology

Piper Brings Laravel-Style Array and String Helpers to PHP 8.5 Pipe Operator

Piper introduces Laravel-inspired array and string helper functions designed specifically for PHP 8.5's new pipe operator, enabling cleaner and more readable data transformation pipelines.

user avatar

Masood Fareed

1 min read
196
3

article image 1

HP 8.5 introduces a powerful new feature called the pipe operator (|>), which allows developers to pass the result of one expression directly into another function. This enables a more readable, linear, and functional programming style in PHP.

Instead of nesting functions or creating intermediate variables, developers can now build clear data transformation pipelines.

However, most built-in PHP functions and Laravel’s chainable classes were not originally designed for this style. To solve this, Spatie Piper provides a set of Laravel-inspired helper functions that are fully optimized for use with the pipe operator.

 

Working with Plain Values

Unlike Laravel Collections, which rely on wrapper objects, Piper works directly with native arrays and strings.

Each function takes a value, transforms it, and passes it along the pipeline:

use function Spatie\Piper\Arr\{filter, map, values, join};

$result = [1, 2, 3, 4, 5, 6]
    |> filter(fn (int $i) => $i % 2 === 0)
    |> map(fn (int $i) => $i ** 2)
    |> values()
    |> join(', ', ', and ');

// Output: "4, 16, and 36"

This approach removes the need for wrapper objects while keeping the code clean and expressive.

 

Curried Functions Designed for Pipelines


All Piper functions are curried, meaning they return a callable when configured. This callable then receives the piped value.

For example:

filter(fn (int $i) => $i % 2 === 0)

does not execute immediately. Instead, it returns a function that will filter the array when it receives data from the pipe.

 

 

String Transformations in Pipelines
Piper also includes string utilities that work seamlessly in the same pipeline:

use function Spatie\Piper\Str\{lower, replace};

$greeting = 'Hello, World!'
    |> lower()
    |> replace('world', 'Piper');

// Output: "hello, piper"
 

Mixing Arrays and Strings
One of the most powerful features is the ability to mix arrays and strings in a single pipeline:

use function Spatie\Piper\Arr\{map, join};
use function Spatie\Piper\Str\{upper, prefix};

$headline = ['laravel', 'news']
    |> map(fn (string $word) => ucfirst($word))
    |> join(' ')
    |> upper()
    |> prefix('>> ');

// Output: ">> LARAVEL NEWS"

This flexibility makes Piper ideal for modern functional-style PHP development.

 

Installation

Install Spatie Piper via Composer:

composer require spatie/piper

Then import the required functions from:

  • Spatie\Piper\Arr
  • Spatie\Piper\Str

 

 

 

 

 

 

 

 


Discussion

Comments (0)

Sort By:

No comments yet.

Related Topics

More For You