Categories


Loading feed
Loading feed
Loading feed

LINQ for PHP (Language Integrated Query for PHP)


On his blog, Maarten Balliauw started a new class library mimicing C#'s LINQ technology.

Perhaps you have already heard of C# 3.5's "LINQ" component. LINQ, or Language Integrated Query, is a component inside the .NET framework which enables you to perform queries on a variety of data sources like arrays, XML, SQL server, ... These queries are defined using a syntax which is very similar to SQL.


There is a problem with LINQ though... If you start using this, you don't want to access data sources differently anymore. Since I'm also a PHP developer, I thought of creating a similar concept for PHP. So here's the result of a few days coding:


PHPLinq - LINQ for PHP - Language Integrated Query

Let's say we have an array of strings and want to select only the strings whose length is < 5. The PHPLinq way of achieving this would be the following:

// Create data source
$names = array("John", "Peter", "Joe", "Patrick", "Donald", "Eric");
    
$result = from('$name')->in($names)
               ->where('$name => strlen($name) < 5')
               ->select('$name');

Feels familiar to SQL? Yes indeed! No more writing a loop over this array, checking the string's length, and adding it to a temporary variable. Read more about this and get familiar with this useful technology.

Comments


Thursday, March 20, 2008
GREATE !
7:19AM PDT · niniwzw
Wednesday, March 26, 2008
HM
5:52PM PDT · littleantyant
Thursday, April 17, 2008
GREAT PIECE OF CODE
5:09AM PDT · avaranger