Quantcast
Viewing latest article 6
Browse Latest Browse All 20

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.

The post LINQ for PHP (Language Integrated Query for PHP) appeared first on Zend Developer Zone.


Viewing latest article 6
Browse Latest Browse All 20

Trending Articles