Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone
How to use linq in PHP

http://commons.wikimedia.org/wiki/File:PHP-logo.svg

My main programming background over the past few years was .NET with C#. Linq is one of the top feature that C# supports and it is very easy to work when you have flexible language feature such as Linq. Linq (Language Integrated Query) provides you a very powerful query capabilities over any data collection. I believe majority of the logic we write extensively depend on iterations, aggregates, either to find particular element of interest of to perform some operation on those items. We have been using SQL for many many years, which is a powerful declarative language and we know the strength of it; it can perform a task in single statement whereas normal programming language would require hundreds of lines of code to perform the same functionality. It is a shame that any of these programming languages used mostly by average developers in average projects (I’m talking about developers like you and me who visit this site to check on simple thing) never tried to implement this SQL like language feature until few year ago ( I mean as a part of the language itself ).  But relational databases and SQL were there for many decades now. Both these areas heavily working on data collections, and objects with  fields, yet programming languages still heavily depend on for loops, while loops, counters, if.. then.else inside loops. Things started changing when .NET (C# and VB.NET) introduced Linq. It makes easier to work with collections like in SQL database records.

Recently I wanted to learn PHP and first thing I realised was that everything in PHP is arrays, so it is a prime candidate for Linq support. Unfortunately no such feature in the core language. As I faced more and more arrays to work with I desperately wanted to find Linq implementation for PHP. I found many though most of them are not maintained as of now and few years since last update so it was very hard to find good and active implementation. Here I will list what I found and be honest I didn’t try any of them except the one that I chose to use mainly because those are not active as of now. It is up to you to select any of these implementations but my purpose is to give you set of tutorials on how to use Linq in PHP (using the library I selected) as I believe there are lot of PHP developers who didn’t want to try out Linq as it was not core part of the language itself. But I think it is something worth trying even with third party implementation. Once you learn how to use Linq you will save lot of coding time as well as re-coding most common logic (ex: aggregate functions) code is much cleaner and easy to read, it shows what it does not how to do it (difference between declarative and procedural languages).

I also want to say this is my beginning of learning PHP so I’m not competent in PHP so you may find lot if wrong usage in my PHP and you’re very much welcome  to correct me guide me in these areas. My purpose here is to learn Linq in PHP together with you. Also note that many can argue that these features may degrade performance, but actually to notice such performance downgrade in current hardware and the datasets average projects use is negligible in my opinion. Moreover if we’re trying to save that fraction of a millisecond then probably we’re implementing something so critical that need to find totally different language which is closer to hardware.

1. PHPLinq

http://phplinq.codeplex.com/

PHPLinq (Linq for PHP) http://phplinq.codeplex.com/

I’m actually not sure there is github branch currently active for this. Only place I found this is in here (http://phplinq.codeplex.com/).  As you can see no updates since 2009 and I couldn’t find any documentation as well. There is a example page and major concern I had was inactivity of the development. If anyone knows whether there is a new active location (github) please inform.

2. Phinq

This seems to be bit up to date however I couldn’t see any development since 2012. According to github readme they have fair amount of functions covered in their implementation. However I’m hesitant to use when I don’t see much of activity for few years. Linq in .NET has been improving every release and introducing new functions with every release so if I use something similar in PHP it should be active.

3. Plinq

Plinq (http://plinq.codeplex.com/)

Plinq (http://plinq.codeplex.com/)

Once again this is not being developed for past few years and moreover it is lack of most useful and simple methods like Sum() Average(), in overall this has very limited number of functions provided. So it is no for me.

4. Final and My Choice – YaLinqo

There are so many reasons for choosing YaLinqo. First of all it is currently active and has implemented all most all the functions and structures as in .NET and they keep on adding every new feature comes with new release of .NET. Well commented and explained code. Plenty of unit tests not only prove that code is stable but also help you to learn its usage. (that’s how I started understanding PHP version of Linq :) as I’m totally new to PHP). It supports different syntax to write same logic which comes very much closer to .NET Linq usage and I agree that it is hard to support as natural as it could be when language it self doesn’t support natively as in .NET. This has been very good effort by the developer to make it similar to .NET (C# or VB.NET) syntax.

So I will be starting discover and learn this PHP implementation of Linq using YaLinqo in coming days, I’m planning to write series of tutorials on how to use it while learning it. Hope you can enjoy it too.


Posts of this series
How to use Linq in PHP - Part 01
How to use Linq in PHP - Part 02 - Setup
How to use Linq in PHP - Part 03 - Basic Operations
How to use Linq in PHP - Part 04 - Basic Operations
How to use Linq in PHP – Part 05 – Sorting
Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone