Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 6 years ago .

Which LINQ do you mean? LINQ to Objects? LINQ to SQL? Entity Framework? LINQ to datasets? Matthias Apr 26, 2011 at 15:12 I mean on language-integrated query, to query any collections, not on technology or platform that contains linq in its name eomeroff Apr 26, 2011 at 15:18 "query any collections" - that's called LINQ to Objects then (see msdn.microsoft.com/en-us/library/bb397919.aspx ). Many people, however, associate ORMs with "LINQ". Matthias Apr 26, 2011 at 15:26 The number and quality of the answers mean this is a very useful question. Judging by the dates, it's been reopened and reclosed already. It should be left open because clearly there are good answers Panagiotis Kanavos Sep 27, 2016 at 13:43 I have voted it up, but later found out that YaLinqo is not exactly production-ready. No documentation, unexpected behaviour. It works, but I decided against it after trying it. Can not remove my vote, SO locked it. Switched to PINQ. Dima Stefantsov Feb 25, 2016 at 7:09 @DimaStefantsov Could you elaborate on "not production ready", "unexpected behavior" and especially on "no documentation"? Almost all complaints on "unexpected behavior" of YaLinqo tend to come from developers who don't read documentation of "toArray" method (it collapses equal keys, thus some results may "disappear"). YaLinqo's reference documentation is the best, but Pinq's introductory articles are better. Pinq would have been great if there was at least one query provider, but there wasn't any progress since MySQL demo's introdiction. :( I hope TimeToogo finds some time for it eventually. Athari Apr 13, 2016 at 5:49 @DimaStefantsov If you manage to modify collections using YaLinqo, please report a bug, I'd really appreciate. :) And it preserves keys too, like Pinq. You can ignore them or discard using toList (or toDictionary(value, increment) ) or, if a method has resultSelectorKey argument, pass increment function to it. In case of Pinq, you can use methods reindex and indexBy , if I'm not mistaken. Though really, just ignoring keys is usually the simplest option. The reason for preserving keys is that sometimes they're necessary, so discarding information has to be explicit. Athari Apr 13, 2016 at 9:24

(YaLinqo developer here.)

Currently, there are three major implementations of LINQ in PHP:

  • YaLinqo — the most minimalistic library architecture-wise (4 classes), but the most featureful, the most performant, with the best documentation and the only one supporting "string lambdas".

  • Ginq — a library of average size (70 classes), on par with YaLinqo in the number of functions, around 1.5–3x times slower, contains custom collections, iterators and comparers, documentation contains argument types at best.

  • Pinq — a huge library (500 classes), supports parsing PHP and transforming into SQL and everything else, much slower than the rest, has a pretty website, but its documentation is average and functionality is lacking.

  • Other libraries aren't worth being mentioned (um, okay, LINQ for PHP, Phinq, PHPLinq and Plinq). They're barely tested, evaluations in them aren't lazy, they rely on weird concepts alien to both PHP and .NET developers. PHPLinq is the only one which actually supports databases, but it's more like DAL for generating SQL with a fixed call order rather than LINQ.

    If you ask me what library to choose, I'd just say to use YaLinqo if you need to work with arrays and objects and any ORM library (not LINQ) when you need to work with databases. But I'll try to explain why.

    Performance

    YaLinqo is by far the fastest of the libraries. It's designed to be fast: it relies only on generators (which produce the fastest iterators); it uses only arrays (no custom collections implemented as wrappers around arrays). Its evolution is getting rid of slow and outdated features: removing custom collections, removing explicit iterators; and worsening code quality if it improves performance: choosing between multiple sort functions instead of using one generic solution, copy-pasting code to reduce the number of function calls.

    Ginq and Pinq took another approach, they use explicit iterator classes. It bites a big chunk out of performance, but allows using iterators separately from fluent method syntax.

    Furthermore, they both have performance traps . When you use property accessors syntax in Ginq, your code becomes 5 times slower. There're also surprises awaiting you when you try using arrays as keys. When you use joining in Pinq, your code becomes hundreds or thousands times slower (I'm not joking, see links below) Performace of joining in Pinq was fixed after my bug report.

    With YaLinqo, it's simpler: either it doesn't work (like arrays in keys), or it works with expected performance. Version 1 did have some hacks to make it possible, like in original LINQ, but the current version doesn't. It may require some adjustments: for example, instead of using an equality comparer, you'll need to produce keys which are equal in the same cases.

    See articles: LINQ for PHP comparison: YaLinqo, Ginq, Pinq on CodeProject, LINQ for PHP: speed matters on Habrahabr (Russian). They cover YaLinqoPerf , git repository with performance tests comparing raw PHP, YaLinqo, Ginq and Pinq.

    Features

    The number of LINQ methods in YaLinqo and Ginq, as well as their functionaility, are pretty close. I'd say there's no clear winner, as both libraries provide methods the other one doesn't have. They are mostly extra methods unavailable in the original LINQ, so I wouldn't worry about it too much.

    Pinq looks like a deserted town. Methods are barebones and are often barely usable. While writing peformance tests, I often had to resort to custom more complex solutions for Pinq, while code for YaLinqo and Pinq usually differed only in method names (different naming conventions: "desc" vs. "descending" and things like that).

    On the other hand, Pinq is the only one which supports parsing PHP and generating SQL from it. Unfortunately, the only query provider is for MySQL and its state is a "demonstration". So, while Pinq has this unique feature, it can't be used yet, unfortunately.

    If you want LINQ to databases to become a reality, I guess you have no choice but to start on working on a query provider for Pinq. It's a very complex task, and I doubt one developer is able to produce high-quality query providers for all databases alone.

    What Ginq has that YaLinqo doesn't is more advanced architecture. Where Set and Dictionary classes are used in Ginq, you'll see arrays and only arrays in YaLinqo. Where Comparer and EqualityComparer are used in Ginq, you'll see closures (or no equivalent) in YaLinqo. At the core, it's a design decision — whether library should use concepts natural for programmers in this language or for programmers used to the library in other languages. The libraries just made a choice.

    It should be noted that more complex architecture doesn't equal a good implementation. Ginq uses public function hash($v) { return sha1(serialize($v)); } for calculating key hashes in "sets", for example.

    Documentation

    YaLinqo stands out with a good reference documentation in PHPDoc and online (generated from PHPDoc). It's mostly documentation of LINQ in .NET from MSDN adapted to PHP. If you know what MSDN is, you know its quality.

    Ginq's documentation is almost non-existent, it usually contains just argument type hints.

    Pinq's documentation is relatively good (every major method has a sentence or two explaining what it does), but it's no match for YaLinqo's documentation.

    Both Ginq and Pinq have good introductory artcicles on the web which explain concepts to new developers. YaLinqo doesn't have any introductory documentation besides a crazy example in ReadMe which doesn't explain anything.

    Pinq has a pretty website too, the only one of the three libraries.

    Everything else

    All three libraries have good test coverage, Composer integration, permissive open-source licenses, and other properties of libararies which are ready to be used in production.

    For those who care about ancient PHP versions, YaLinqo 1.x requires PHP 5.3, YaLinqo 2.x requires PHP 5.5, Ginq requires PHP 5.3, Pinq requires PHP 5.4.

    P.S. If you have any additions, or think I'm biased, please comment. Comments are what I miss after writing so much text. :)

    It makes me sad to see an accepted answer which promotes writing php code inside string variables like where('$name => strlen($name) < 5') ... I don't even want to imagine the pain experienced when having to rename any class, property, variable, etc. The proper way to do things would be using real language constructs (delegates/anon funcs), like ginq/pinq do. Mladen B. Jun 20, 2018 at 10:06

    A lot has changed in the PHP world over the last couple of years rendering most of the previous answers out of date.

    Here is a new comparison table of the main LINQ implementations for PHP:

    These libraries can all be installed via composer .

    In summary I would recommend the PINQ library (I am biased as I am the author) because it is actively maintained, well documented and tested and provides an implementation of true LINQ in PHP.

    By true LINQ I mean that the library is not only a fluent collection API for in-memory arrays but also implements real query parsing with expression trees. This allows the integration of this API with external data sources hence P HP In tegrated Q uery. A demo of such functionality can viewed here where queries are being compiled into SQL and run against a MySQL database:

    (YaLinqo dev.) It's great to see some progress in this area! I'm totally confused by how you manage to parse syntax trees from anonymous functions (?), I'll need to look into your code. I have a few objections though. (1) Comparing number of tests isn't fair, some libraries group tests into functions (due to laziness usually). Sure, YaLinqo doesn't contain 3000 tests, but it contains around 600 tests, not 168, so, when taking library size and scope into account, it isn't that small. (2) I would be very interested in performance comparisons. Athari May 19, 2015 at 0:49 I've tried comparing YaLinqo with Ginq, and YaLinqo turned out to be 2 times faster on average ( yield vs manually implemented iterators is a huge difference perf-wise), and the gap is even bigger in tests where Ginq uses weird inefficient code. Haven't explored your implementation yet. That being said, YaLinqo intentionally excludes features which would be too inefficient in PHP, I've even removed some features when porting to generators. Also, there's a huge drop in performance compared to manually written code, no matter what library is used... Athari May 19, 2015 at 0:49 (3) I find it funny that the only query provider implementation is for MySQL and it's demo (correct me if I'm wrong). The complexity of implementing a full-featured database query provider is one of the reasons I haven't even looked into that direction. (4) Your site is nice, but I don't see anything comparable to MSDN-like PHPDoc for all functions (did I miss it?). I totally agree that YaLinqo needs a website though. Athari May 19, 2015 at 0:49 (5) The range of colors in the maintenance column is funny, but I won't deny that YaLinqo isn't being actively developped. :) However, I consider it featureful enough and don't see any ways to significantly improve it (within its scope). There're some ideas for additional methods, but there's no activity in the issue tracker, so I don't care. Athari May 19, 2015 at 0:49