Made of Everything You're Not

Thoughts on programming, people and life
  • Home
  • Projects
  • Portfolio
  • Resume
« WP-Click-Track 0.6: Going Native
Looking For More From Zend Framework »

PHPLinq: PHP Language Integrated Query

I admit, sometimes I get language envy. Don’t get me wrong, I love php, but every once in a while I’ll hear about a feature in another language that just gets me going. The C# Reflection implementation, IntelliSense and LINQ immediately come to mind. I really want to get a look at these features and play with them but, alas, I just haven’t yet. (Honestly, I’m just not that good at C# yet. Soon though, very soon.)

PHPLinq: PHP Integrated Language Query

PHPLinq: PHP Integrated Language Query

BTW, yes I know php has Reflection but, the way I understand it, the .NET Reflection implementation kicks the crap out of the php one. Get over it.

Now it looks like there’s a quasi-sorta-kinda implementation of LINQ done in PHP with the totally original title of PHPLinq. It’s not a true port of LINQ, being that it deals with strings and isn’t done at the language level, but it’s still pretty compelling in it’s own right.

If you don’t know what LINQ is all about it stands for Language Integrated Query and is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.

That basically means it allows you to query stuff, similar in concept but different in practice to how you would query a database. PHPLinq works well for searching an array of objects using a SQL like 0bject syntax.

There’s a great tutorial on Switch on the Code that goes into some detail on how to use PHPLinq with a good example of usage. To help pique your interest the below should give you a reason to check it out; the code below searches an array for all the strings in an array with a string length less than 5 characters:

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

Notice how it’s laid out like a database abstraction? Pretty cool, right?

The really exciting part about PHPLinq is that it can work off of lots of data sources like XML, RSS and a database table (more on this in a minute). Here’s an example taken from the unit tests for PHPLinq:

<?php
$rssFeed = simplexml_load_string(file_get_contents('http://blog.maartenballiauw.be/syndication.axd'));
$result = from('$item')->in($rssFeed->xpath('//channel/item'))
			->orderByDescending('$item => strtotime((string)$item->pubDate)')
			->take(2)
			->select('new {
							"PostTitle" => (string)$item->title,
							"PostAuthor" => (string)$item->author,
							"MetaData" => new {
												"Url" => (string)$item->link,
												"Guid" => (string)$item->guid,
												"PostDate" => strtotime((string)$item->pubDate)
										  }
					  }');
 
print_r($result);
?>

The above will return only the post title, author and whatever meta data the feed contains.

When I think about the database option I get a headache though; I can’t, for the life of me, think of when I would want to use PHPLinq to search a return database call. If I understand right the flow would work as a) grab all database rows and b) use PHPLinq to search through those rows.

This idea goes against everything I’ve ever known about SQL and resource management. It’s sacrilegious to grab all the rows in a table and just plain silly to use a separate layer of the program to parse those results. What possible argument could there be for not using plain old SQL (or even a SQL generator) and just let MySQL do the heavy lifting.

Admittedly, I haven’t found any code samples for how the SQL portion would work so I may be way off base here.

Either way, PHPLinq is still a compelling module to help sifting through array sets and XML. It should make searching files a lot easier too. Even if that’s not enough PHPLinq is still worth checking out if for no other reason an example of an interesting idea done in php.

Bookmark and Share

Related Posts

Stand Alone ExpressionEngine Authentication
Importing Legacy Users Into ExpressionEngine
CartThrob 2.0 Beta Fun
ExpressionEngine and the Mystery of M00o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr
Custom Routes With Zend Framework

Tags: php, phplinq

This entry was written by Eric Lamb and posted on Friday, August 21st, 2009 at 5:00 am and is filed under Code, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments

  1. abcphp.com says:
    January 7, 2010 at 9:43 pm

    PHPLinq: PHP Language Integrated Query | Made of Everything You’re Not | Eric Lamb…

    I admit, sometimes I get language envy. Don’t get me wrong, I love php, but every once in a while I’ll hear about a feature in another language that just gets me going. The C# Reflection implementation, IntelliSense and LINQ immediately come to mind. I…

    Reply
  2. za szybki orgazm says:
    July 20, 2011 at 8:34 am

    Hi, nice site. Thx

    Reply

Leave a Reply

Click here to cancel reply.

  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008

Copyright © 2008 - 2012 Eric Lamb - All rights reserved