PHP 8.5’s pipe operator |> offers a fresh syntax for chaining operations, yet Laravel developers have long relied on collect()->filter()->map() for transformations. The recent launch of Spatie’s Piper—a library that adapts Laravel’s helper functions for the pipe operator—has reignited the debate. Is Piper a practical upgrade, or just syntactic sugar? The answer depends on specific use cases and constraints.
How the Pipe Operator Works in PHP 8.5
The pipe operator |> passes the result of the left expression directly as the first argument to a callable on the right. For example:
$result = ' laravel ' |> trim(...) |> strtoupper(...); // Output: "LARAVEL"Each step executes sequentially, top to bottom, without intermediate variables. PHP 8.1’s first-class callable syntax (...) enables this behavior. However, PHP’s standard library functions are not natively compatible with the pipe operator due to inconsistent argument orders. For instance, array_map($callback, $array) expects the callback first, while array_filter($array, $callback) requires the array first. Piper eliminates this friction by wrapping these functions into a Laravel-friendly toolkit.
What Piper Brings to the Table
Piper, now at version 1.0, requires PHP 8.5 and reimagines Laravel’s array and string helpers as standalone functions under the Spatie\Piper namespace. Unlike Laravel Collections, Piper’s functions are higher-order, meaning they return closures ready to receive a single piped value:
use function Spatie\Piper\Arr\{filter, map, join};
use function Spatie\Piper\Str\{prefix, suffix};
$summary = [1, 2, 3, 4, 5, 6]
|> filter(fn (int $i) => $i % 2 === 0)
|> map(fn (int $i) => $i ** 2)
|> join(', ', ', and ')
|> prefix('The winning numbers are ')
|> suffix('.');
// Output: "The winning numbers are 4, 16, and 36."Notably, Piper avoids the overhead of Collections by working directly with plain arrays and strings, eliminating conversions like collect($array) or ->all(). This is particularly useful in scenarios where third-party libraries or APIs expect native array types.
Three Scenarios Where Piper Outperforms Collections
1. Native Value Handling Piper’s functions accept and return native arrays or strings, making them ideal for codebases that frequently interact with plain arrays—such as SDKs, JSON payloads, or array-typed function signatures. Collections, by contrast, wrap and unwrap data at every step, adding unnecessary overhead.
2. Seamless Integration of Custom Functions Piper’s flexibility allows mixing Laravel helpers, native PHP functions, and domain-specific logic into a single chain. Collections restrict you to methods defined on the Collection class or globally registered macros. For example:
$total = $orders
|> filter(fn (Order $o) => $o->isPaid())
|> map(fn (Order $o) => $o->total)
|> array_sum(...)
|> $this->applyDiscount(...);Here, applyDiscount() can be any callable, including a class method, without requiring workarounds like macros or breaking the chain.
3. Zero Framework Dependency Piper’s standalone functions require no Laravel components, making it a lightweight choice for package authors or projects aiming to reduce dependency bloat. Developers can leverage Laravel-style helper functions without pulling in illuminate/collections.
Where Laravel Collections Remain Superior
1. Extensive API Surface While Piper ports popular helpers, Collections offer over 100 optimized methods—groupBy(), pluck(), chunk(), zip(), and higher-order messages like ->map->title(). Piper’s current scope makes it impractical for complex transformations that rely on these advanced features.
2. Lazy Evaluation for Performance LazyCollection processes large datasets (e.g., database cursors, CSV files) with minimal memory usage by yielding items one at a time. Piper, however, materializes a new array at each step, which can lead to performance bottlenecks when chaining multiple operations over large datasets.
3. Framework Synergy Laravel’s ecosystem is built around Collections. Query builders, Eloquent relations, and many APIs return Collections by default. Forcing a conversion to a plain array to use Piper disrupts this workflow and adds unnecessary steps.
4. Developer Experience Collections provide IDE autocompletion and discoverability—type -> and explore available methods instantly. Piper requires manual imports and knowledge of function names, adding a small but consistent cognitive load.
The Deciding Factor: PHP 8.5 Adoption
Piper’s most significant limitation isn’t functional—it’s compatibility. PHP 8.5, which introduced the pipe operator, launched in November 2025. Most production environments still run PHP 8.2 to 8.4, making Piper inaccessible for the majority of Laravel applications today. Until PHP 8.5 adoption accelerates, Piper remains a niche tool for bleeding-edge projects or local development environments.
For teams already on PHP 8.5, Piper offers a compelling alternative for simple, high-performance transformations. However, Laravel Collections continue to dominate in scenarios demanding deep functionality or framework integration. The choice ultimately hinges on the project’s PHP version, performance needs, and architectural preferences.
AI summary
PHP 8.5’nin pipe operatörüyle tanıttığı yeni fonksiyonel yaklaşım, Spatie’nin Piper kütüphanesiyle daha da güçlendi. Peki, koleksiyon zincirlerinin yerini alabilecek mi? Avantajları ve sınırlarıyla detaylı inceleme.