LINQ for PHP (Language Integrated Query for PHP)
Edit on GitHubPerhaps 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
A basic example
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:
[code:c#]
// Create data source
$names = array("John", "Peter", "Joe", "Patrick", "Donald", "Eric");
$result = from('$name')->in($names)
->where('$name => strlen($name) < 5')
->select('$name');
[/code]
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.
You may have noticed something strange... What's that $name => strlen($name) < 5 doing? This piece of code is compiled to an anonymous function or Lambda expression under the covers. This function accepts a parameter $name, and returns a boolean value based on the expression strlen($name) < 5.
An advanced example
There are lots of other examples available in the PHPLinq download, but here's an advanced one... Let's say we have an array of Employee objects. This array should be sorted by Employee name, then Employee age. We want only Employees whose name has a length of 4 characters. Next thing: we do not want an Employee instance in our result. Instead, the returning array should contain objects containing an e-mail address and a domain name.
First of all, let's define our data source:
[code:c#]
class Employee {
public $Name;
public $Email;
public $Age;
public function __construct($name, $email, $age) {
$this->Name = $name;
$this->Email = $email;
$this->Age = $age;
}
}
$employees = array(
new Employee('Maarten', '[email protected]', 24),
new Employee('Paul', '[email protected]', 30),
new Employee('Bill', '[email protected]', 29),
new Employee('Bill', '[email protected]', 28),
new Employee('Xavier', '[email protected]', 40)
);
[/code]
Now for the PHPLinq query:
[code:c#]
$result = from('$employee')->in($employees)
->where('$employee => strlen($employee->Name) == 4')
->orderBy('$employee => $employee->Name')
->thenByDescending('$employee => $employee->Age')
->select('new {
"EmailAddress" => $employee->Email,
"Domain" => substr($employee->Email, strpos($employee->Email, "@") + 1)
}');
[/code]
Again, you may have noticed something strange... What's this new { } thing doing? Actually, this is converted to an anonymous type under the covers. new { "name" => "test" } is evaluated to an object containing the property "name" with a value of "test".
This all sounds intuitive, interesting and very handy? Indeed! Now make sure you download a copy of PHPLinq today, try it, and provide the necessary feedback / feature requests on the CodePlex site.
This is an imported post. It was imported from my old blog using an automated tool and may contain formatting errors and/or broken images.
21 responses