LINQ for PHP 5.3.0

Posted by Neil on 2nd August 2008

This is really just a quick note to announce that I've begun work on an implementation of LINQ for PHP 5.3.0, which has recently attained alpha status. While not a complete work as yet, it's already quite functional.

Features

The class takes advantage of the new closure support to avoid the need for evaluating strings of code. It can operate on any data source that may be iterated and currently supports joins, where clauses and ordering.

Example

The following code joins two data sources and selects people who prefer red fruit, returning their names and the names of the fruit, ordering alphabetically on the people's names.

$fruits = array(
#	new Fruit(name, colour, shape)
	new Fruit('banana', 'yellow', 'long'),
	new Fruit('orange', 'orange', 'round'),
	new Fruit('grape', 'red', 'round'),
	new Fruit('grape', 'green', 'round'),
	new Fruit('apple', 'red', 'round'),
);

$people = array(
#	new Person(name, fruit)
	new Person('Barry', 'banana'),
	new Person('Eddie', 'orange'),
	new Person('Jason', 'banana'),
	new Person('Lawrence', 'apple'),
	new Person('Michael', 'grape'),
	new Person('Mitch', 'orange'),
	new Person('Sally', 'banana'),
	new Person('Carl', 'grape'),
);

$l = new LINQ;
$result = $l	->from($fruits, 'fruits')
		->join($people, function ($a, $b) { return $a->name === $b->fruit; }, 'people')
		->where(function ($obj) { return $obj->colour === 'red'; })
		->orderBy('people.name', LINQ::ASC)
		->select('fruits.name', 'people.name');

# Result:
Array
(
    [0] => stdClass Object
	(
	    [fruits.name] => grape
	    [people.name] => Carl
	)

    [1] => stdClass Object
        (
            [fruits.name] => apple
            [people.name] => Lawrence
        )

    [2] => stdClass Object
        (
            [fruits.name] => grape
            [people.name] => Michael
        )
)

Code

It's still under development and I'm not quite ready to release yet, but it shouldn't be long. In the meantime, you can email me if you have any questions.