I am a big fan of LINQ (if you don't know what it is check out our Intro to LINQ Tutorial in C#) and I just started playing with an interesting php library, PHPLinq (created by Maarten Balliauw), for integrating a LINQ-like syntax into PHP. This tutorial is going to go over the basics of using this library and some of the cool things that can be done with it.
To show this off we are going to put together a simple contacts example. The example creates an array of contacts and then we will use phpLinq to pull out certain contacts based on where they are from. One of the first things we need to do is setup the php page to use the library. Once you have the library downloaded we will first create a super easy example, copied from the PHPLinq site. I want to point out a couple items in this first code block. The first command at the top adds the library directory to the php include path, enabling all the files in the library folder to be used. The second command includes the a file from the library needed to use the linq syntax.
include_once('PHPLinq/LinqToObjects.php');
$names = array("John", "Peter", "Joe", "Patrick", "Donald", "Eric");
$result =
from('$name')->in($names)
->where('$name => strlen($name) < 5')
->select('$name');
After including the needed files, an array is created to hold the data we are going to query. The second statement is the actual query syntax. This should look somewhat similar to typical SQL syntax with a few quirks. The great thing with this is that there isn't a need for a bunch of loops or temporary variables anymore.
One interesting part of the above code is the where expression - where('$name => strlen($name) < 5'). This is actually an anonymous function. This is very close to the lambda syntax in C# (you can learn more about that from our tutorial on Lambda Expressions in C#). The expression $name => strlen($name) < 5 means that we pass in the $name variable and return the answer from the Boolean expression strlen($name) < 5.
The rest of the query is pretty easy to understand. The query looks at all the single $name variables in the array $names and returns the ones that have a length of less than 5.
The next example is slightly more complex - it does contact lookups based upon a user request. Below you can see an iframe with the results of the php code running against a request for the results of Ohio:
We will start with a basic class declaration for a contact:
{
public $name;
public $address;
public $city;
public $state;
public function __construct($name, $address, $city, $state)
{
$this->name = $name;
$this->address = $address;
$this->city = $city;
$this->state = $state;
}
}
The next thing we need to do is include the same two pieces we did in the first example and also our Contact class php file. Next an array is created to hold the data we are going to work with. Next we are going to query the data for all the contacts that live in a particular state. The state is determined by the request variable named state. For my example I passed the state in through the url as a parameter. The contacts will be ordered by name using orderBy. You can see this on the demo page.
include_once('PHPLinq/LinqToObjects.php');
include_once('contact.php');
// Create data source
$contacts = array(
new Contact("Chuck", "523 Fake Address", "Cincinnati", "OH"),
new Contact("Steve", "0991 Another St.", "L.A.", "CA"),
new Contact("Alex", "7912 Bs Blvd.", "L.A.", "CA"),
new Contact("Kevin", "1092 Dugg St.", "San Francisco", "CA"),
new Contact("Mike", "821 Easy St.", "Indianapolis", "IN"),
new Contact("Caroline", "9012 Good St.", "Cincinnati", "OH"),
new Contact("Brandon", "2459 Loser Dr.", "East Boofoo", "IN")
);
$result =
from('$contact')->in($contacts)
->where('$contact =>
$contact->state == "' . $_REQUEST['state'] . '"')
->orderBy('$contact => $contact->name')
->select('$contact');
This leaves us with all the contacts living in the requested state. The $result will just be an array of all the contacts that meet the where criteria. We can then do whatever we would like with them. In my case I just print them out in a table.
<table>
<thead>
<tr>
<td>Name</td>
<td>Address</td>
<td>City</td>
<td>State</td>
</tr>
</thead>
<tbody>
<? for($i = 0; $i < count($result); $i++): ?>
<tr class="datarow<?= ($i & 1)?>">
<td><?= $result[$i]->name ?></td>
<td><?= $result[$i]->address ?></td>
<td><?= $result[$i]->city ?></td>
<td><?= $result[$i]->state ?></td>
</tr>
<? endfor; ?>
</tbody>
</table>
</div>
That pretty much covers the basics of PHPLinq, although there are many more advanced features that I may dig into in the future. Let me know what you all think about the library. I also want to point out I have talked to the creator and he is still actively working on it which is always a good sign for open source libraries. You can also grab the source for the contacts example. If there are any questions feel free to drop a comment.
06/20/2008 - 05:18
Except this isn't LINQ, because LINQ means Language _INtegrated_ Query.
This is more lite SINQ, String INtegrated Query, and it blows.
06/20/2008 - 08:38
What about it don't you like? I mean it isn't all that different from LINQ.
06/21/2008 - 12:57
Except the fact that the original LINQ is a part/an extension of the .NET languages/syntaxes and this PHP-LINQ isn't anything like it, now is it?
12/18/2008 - 01:41
I think linq statements are redundant as well. Why both ‘for’ and ’select’?
10/01/2009 - 00:29
So Micro$oft actually created something that PHP users are jealous about? hhhmmm sounds like PHP is working too hard at this. PHP, please let it be.
The idea behind linq for sql in .net framework is the fact that you get this complete type safe system in which the db layer is exposed to the IDE through a class making it "easier" to code a sql statement the safe way. and in many ways cut the amount of time that we use to develop a complete db layer.
This PHP attempt is weak..
11/12/2009 - 16:51
As i understand it, Linq is not just for SQL it is also for other objects, XML and arrays.
If it is just for DB, Hibernate did beat M$ for it a loong time ago as well as Doctrine for PHP which is excellent btw !
Have anyone tried the performance of PHPLinq versus regular PHP ? will it be worth it ?
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.