saintropeworks / php-linq
A PHP implementation of LINQ-style enumerable operations.
Requires
- php: >=8.5
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-08-24 08:07:57 UTC
README
A modern PHP implementation of Microsoft's LINQ (Language Integrated Query), designed specifically for PHP 8.5+ and optimized for composable, pipeline-based query construction.
PHP LINQ is intended to provide a familiar LINQ programming model to PHP developers while taking advantage of modern PHP language features rather than attempting to maintain compatibility with legacy PHP versions.
Requirements
- PHP 8.5 or later
- Composer
PHP LINQ is intentionally not designed for backwards compatibility with older PHP versions. The library targets modern PHP and makes use of language features available in PHP 8.5+, including the pipeline operator (|>).
Installation
Install PHP LINQ through Composer:
composer require saintropeworks/php-linq
Example
use function SRW\LINQ\{ Where, Select, Average }; $averageAge = $users |> Where(fn($user) => $user->isActive) |> Select(fn($user) => $user->age) |> Average();
LINQ Functions
LINQ functions are imported using PHP's use function syntax, allowing you to pull in only the operations you want to use:
use function SRW\LINQ\{ Where, Select, Average };
The goal is to provide the full C# LINQ operation set in PHP. Where PHP's lack of method overloading makes a direct one-to-one translation impossible, separate function names are used to represent the different overloads.
For example:
Where— The standardFunc<TSource, bool>predicate.WhereWithIndex— The overload usingFunc<TSource, int, bool>.
This same approach is used throughout the library where necessary to preserve the functionality of the corresponding LINQ overloads.
How It Works
All of the functions exposed through SRW\LINQ are essentially partial-function wrappers around the corresponding functions provided by Enumerable.
There is nothing stopping you from using Enumerable directly:
use SRW\LINQ\Enumerable; $filteredUsers = Enumerable::Where( $users, fn($user) => $user->isActive ); $selectedUsers = Enumerable::Select( $filteredUsers, fn($user) => $user->age ); $averageAge = Enumerable::Average($selectedUsers);
However, the point of the function wrappers is to get as close as PHP can reasonably get to the fluent, extension-method style of C# LINQ.
Combined with PHP's pipeline operator (|>), the result is a composable LINQ-style query syntax:
$averageAge = $users |> Where(fn($user) => $user->isActive) |> Select(fn($user) => $user->age) |> Average();
The goal is simple: make LINQ feel like LINQ, while working with PHP rather than fighting against it.