PHP 中是否有相当于 C# 中的 LINQ 的东西?

发布于 2024-11-03 04:23:35 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

久夏青 2024-11-10 04:23:35

尝试 YaLinqo。它是 PHP 的最佳 LINQ。

这是一个比较表:

在此处输入图像描述

Try YaLinqo. It is the best LINQ for PHP.

Here is a comparison table:

Enter image description here

留蓝 2024-11-10 04:23:35

(这里是 YaLinqo 开发人员。)

目前,PHP 中的 LINQ 主要实现有 3 种:

  • YaLinqo — 最简约的库架构(4 个类),但功能最丰富、性能最高、具有最好的文档并且是唯一一个支持“字符串 lambda”的库。

  • Ginq — 一个平均大小的库(70 个类),与 YaLinqo 相当函数数量,大约慢 1.5-3 倍,包含自定义集合、迭代器和比较器,文档最多包含参数类型。

  • Pinq — 一个巨大的库(500 个类),支持解析 PHP 并转换为 SQL 和其他一切都比其他慢得多,有一个漂亮的网站,但其文档很一般并且缺乏功能。

其他库不值得一提(嗯,好吧,LINQ for PHP、Phinq、PHPLinq 和 Plinq)。它们几乎没有经过测试,其中的评估并不懒惰,它们依赖于 PHP 和 .NET 开发人员都陌生的奇怪概念。 PHPLinq 是唯一真正支持数据库的,但它更像是 DAL,用于生成具有固定调用顺序的 SQL,而不是 LINQ。

如果你问我选择哪个库,我会说如果你需要使用数组和对象,我会说使用 YaLinqo;如果你需要使用数据库,我会说使用任何 ORM 库(不是 LINQ)。但我会尝试解释原因。

性能

YaLinqo 是迄今为止最快的库。它的设计速度很快:它仅依赖于生成器(生成最快的迭代器);它仅使用数组(没有作为数组包装器实现的自定义集合)。它的演变是摆脱缓慢且过时的功能:删除自定义集合,删除显式迭代器;如果提高性能,代码质量就会恶化:在多个排序函数之间进行选择,而不是使用一种通用解决方案,复制粘贴代码以减少函数调用次数。

Ginq 和 Pinq 采用了另一种方法,它们使用显式迭代器类。它极大地降低了性能,但允许将迭代器与流畅的方法语法分开使用。

此外,它们都有性能陷阱。当你在 Ginq 中使用属性访问器语法时,你的代码会变慢 5 倍。当您尝试使用数组作为键时,还会有惊喜等着您。 当您在 Pinq 中使用加入时,您的代码会变慢数百或数千倍(我不是在开玩笑,请参阅下面的链接) 在我报告错误后,Pinq 中加入的性能已得到修复。

使用 YaLinqo,它更简单:要么它不起作用(就像键中的数组),要么它可以达到预期的性能。版本 1 确实有一些技巧使其成为可能,就像原始 LINQ 中一样,但当前版本没有。它可能需要一些调整:例如,您需要生成在相同情况下相等的键,而不是使用相等比较器。

请参阅文章:LINQ for PHP 比较:YaLinqo、Ginq、Pinq 在 CodeProject 上,LINQ for PHP:速度很重要 Habrahabr(俄语)。它们涵盖 YaLinqoPerf,这是一个 git 存储库,其中包含对原始 PHP、YaLinqo、Ginq 和 Pinq 进行比较的性能测试。

特性

YaLinqo 和 Ginq 中的 LINQ 方法的数量以及它们的功能非常接近。我想说没有明显的赢家,因为两个库都提供了另​​一个库所没有的方法。它们大多是原始 LINQ 中不可用的额外方法,因此我不会太担心。

Pinq 看起来就像一个废弃的小镇。方法很简单,而且通常几乎不可用。在编写性能测试时,我经常不得不为 Pinq 定制更复杂的解决方案,而 YaLinqo 和 Pinq 的代码通常仅在方法名称上有所不同(不同的命名约定:“desc”与“descending”等)。

另一方面,Pinq 是唯一支持解析 PHP 并从中生成 SQL 的工具。不幸的是,唯一的查询提供程序是 MySQL,其状态是“演示”。因此,虽然 Pinq 具有这个独特的功能,但不幸的是它还不能使用。

如果您希望 LINQ to 数据库成为现实,我想您别无选择,只能开始为 Pinq 开发查询提供程序。这是一项非常复杂的任务,我怀疑一个开发人员是否能够单独为所有数据库生成高质量的查询提供程序。

Ginq 拥有 YaLinqo 所没有的更先进的架构。在 Ginq 中使用 SetDictionary 类的地方,您将在 YaLinqo 中看到数组并且仅看到数组。在 Ginq 中使用 ComparerEqualityComparer 的地方,您将在 YaLinqo 中看到闭包(或没有等效项)。从本质上讲,这是一个设计决策——库是否应该使用对于使用该语言的程序员或习惯于其他语言的库的程序员来说自然的概念。图书馆只是做出了选择。

应该注意的是,更复杂的架构并不等于好的实现。 Ginq 使用 public function hash($v) { return sha1(serialize($v));例如,用于计算“集合”中的密钥哈希值。

文档

YaLinqo 在 PHPDoc 和在线(从 PHPDoc 生成)中提供了很好的参考文档。它主要是 MSDN 中 .NET 中 LINQ 的文档,适用于 PHP。如果您知道 MSDN 是什么,您就知道它的质量。

Ginq 的文档几乎不存在,它通常只包含参数类型提示。

Pinq 的文档相对较好(每个主要方法都有一两句话解释其作用),但它无法与 YaLinqo 的文档相媲美。

Ginq 和 Pinq 在网络上都有很好的介绍性文章,向新开发人员解释概念。 YaLinqo 除了 ReadMe 中的一个疯狂示例之外没有任何介绍性文档,该示例没有解释任何内容。

Pinq 也有一个漂亮的网站,是三个库中唯一的一个。

其他所有

这三个库都具有良好的测试覆盖率、Composer 集成、宽松的开源许可证以及可以在生产中使用的库的其他属性。

对于那些关心旧 PHP 版本的人来说,YaLinqo 1.x 需要 PHP 5.3,YaLinqo 2.x 需要 PHP 5.5,Ginq 需要 PHP 5.3,Pinq 需要 PHP 5.4。

PS如果您有任何补充,或者认为我有偏见,请评论。写了这么多文字之后,评论是我最想念的。 :)

(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. :)

吃→可爱长大的 2024-11-10 04:23:35

在过去的几年里,PHP 世界发生了很多变化,使得之前的大多数答案都过时了。

下面是 PHP 主要 LINQ 实现的新比较表:

LINQ Library Comparison Table

这些库都可以通过 < a href="https://getcomposer.org/" rel="nofollow noreferrer">作曲家。

总之,我会推荐 PINQ 库(我有偏见,因为我是作者),因为它是积极的维护、良好记录和测试,并在 PHP 中提供真正的 LINQ 实现。

true LINQ 我的意思是该库不仅是内存数组的流畅集合 API,而且还使用表达式树实现真正的查询解析。这允许此 API 与外部数据源集成,因此 PHP In集成Query< /em>.可以在此处查看此类功能的演示,其中查询被编译为 SQL 并运行MySQL 数据库:

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:

LINQ Library Comparison Table

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 PHP Integrated Query. A demo of such functionality can viewed here where queries are being compiled into SQL and run against a MySQL database:

静谧幽蓝 2024-11-10 04:23:35

还有 phinq ,它有一个比 PHPLinq 更新的版本,它看起来更像LINQ 到对象 C# 比 PHPLinq 更好。

There is also phinq which has a more recent release that PHPLinq, and it seems to look more like LINQ to Objects for C# than PHPLinq does.

葮薆情 2024-11-10 04:23:35

好的 PHP ORM 库?

这个问题的答案是:

研究教义。

Doctrine 1.2 实现了 Active Record。 Doctrine 2+ 是一个 DataMapper
ORM。

此外,请查看 Xyster。它基于数据映射器模式。

另外,看看 DataMapper 与 Active Record。

Good PHP ORM Library?

An answer to this question says

Look into Doctrine.

Doctrine 1.2 implements Active Record. Doctrine 2+ is a DataMapper
ORM.

Also, check out Xyster. It's based on the Data Mapper pattern.

Also, take a look at DataMapper vs. Active Record.

望笑 2024-11-10 04:23:35

请参阅 PHPLinq(一组模仿 C#3.0 的 LINQ 的 PHP 类)。

See PHPLinq (a set of PHP classes mimicing C#3.0's LINQ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文