Mike Borozdin's Blog

A blog about programming, web and IT in general

Search

 

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© 2008 Mike Borozdin

Is PHPLinq As Cool As Real LINQ?

I read about the PHP Implementation of LINQ called PHPLinq. Frankly, I was sceptical about it. Finally, I gave it a try. I still remain sceptical...


Let’s me explain why. Take a look at this fairly simple example, where we extract all the numbers greater than 5:

<?php

set_include_path(get_include_path() . PATH_SEPARATOR . '../PhpLinq/Classes/');
require_once('PHPLinq/LinqToObjects.php');

$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

$result = from('$number')->in($numbers)
            ->where('$number => $number > 5')
            ->select('$number');

print_r($result);           

?>

If you familiar with LINQ, then you may say: "Wow, that’s cool! Just like in .NET". Yes, the syntax is quite familiar. But if you look closer, you will notice that the query expressions are encloced in single quotes, i.e. they are just string. That makes a significant difference between PHPLinq and real LINQ. Do you remember what LINQ stands for? It stands for Language Intergrated Query. Unfortunately, PHPLinq isn’t a language integrated query, since it’s not supported by the language natively and we have to use strings for writting queries.

When we write real LINQ queries in Visual Studio we’ve got syntax highlighting, we’ve got IntelliSence and that is more important we can track errors at the compilation stage. PHPLinq lacks all these things. Ok, PHP is an interpreted language, so there’s no compilation stage; however there are smart IDEs that track errors while we’re writing code.


I want to illistrate this, let’s make a deliberate error, we’ll change $number to $number1 in the from clause. If you run the script you’ll get a notice, in case notices are enabled and nothing more.But if you are writing this in C#, it won’t even get compiled.


In the real world LINQ is used mostly as a SQL replace, we don’t have to write the queries in strings anymore and catch the exceptions when running an application. But unfortunately we cannot do the same with PHPLinq, it still forces us to put the queries into strings.


Well, I think I was sceptical enough about it, but still PHPLinq has some cool features, just check this example taken from the PHPLinq official web site:

<?php
/**
 * PHPLinq
 *
 * Copyright (c) 2008 PHPLinq
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPLinq
 * @package    PHPLinq
 * @copyright  Copyright (c) 2008 PHPLinq (http://www.codeplex.com/PHPLinq)
 * @license    http://www.gnu.org/licenses/lgpl.txt    LGPL
 * @version    0.3.0, 2008-06-23
 */

/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');

/** PHPLinq_LinqToObjects */
include 'PHPLinq/LinqToObjects.php';

// Custom class
class Employee {
    public $Name;
    public $Email;
 
    public function __construct($name, $email) {
        $this->Name     = $name;
        $this->Email     = $email;
    }
}

// Create data source
$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);


Tags: , ,
Posted by Mike Borozdin on Friday, July 04, 2008 9:45 PM GMT
Add to Technorati Favorites  Kick it!  
Permalink | Comments (8) | Post RSSRSS comment feed

Comments

leppie za

Saturday, July 05, 2008 12:02 PM GMT

leppie

I did the same with Scheme Smile

Mike Borozdin

Saturday, July 05, 2008 12:42 PM GMT

Mike Borozdin

Do you mean the LISP-like functional language?

bonitoo id

Sunday, July 06, 2008 5:07 AM GMT

bonitoo

I would say "Wow, that’s cool! Just like in .NET", but i rather use the classic php string query.

leppie za

Sunday, July 06, 2008 9:25 AM GMT

leppie

Hi Mike

Yes, specifically R6RS Scheme. You can view the implementation here: bazaar.launchpad.net/.../5

Maarten Balliauw be

Tuesday, July 08, 2008 6:44 PM GMT

Maarten Balliauw

Thank's for the review! Integrating it into PHP is quite hard, the LINQ as string choice has erupted for that reason.

You should check out the latest release, it also contains a JOIN example.

Markus Wolff de

Tuesday, July 08, 2008 8:05 PM GMT

Markus Wolff

I want to illistrate this, let’s make a deliberate error, we’ll change $number to $number1 in the from clause. If you run the script you’ll get a notice, in case notices are enabled and nothing more.But if you are writing this in C#, it won’t even get compiled.

Uhm... that's actually not only true for phpLinq, but also for PHP in general. Use an undefined variable, you'll get a notice. Do the same in .NET - no compiling for you tonight! So what you're saying is: Don't use PHP, because it's an interpreted, loosely-typed language?

Aside from that, I never got what the fuss over Linq is all about. If you look at it closely, it almost looks as if you're writing SQL anyway - just that you have to re-learn the syntax for the slight differences, have less readable code, have fewer possibilities for optimizing. For what benefit? Portability? If I cared about that I'd use a proper ORM and not care about raw SQL queries anyway, thank you very much. Linq is another good example for a solution looking for a problem.

Mike Borozdin

Sunday, July 13, 2008 3:55 PM GMT

Mike Borozdin

Markus Wolff,


So what you're saying is: Don't use PHP, because it's an interpreted, loosely-typed language?


Of course, no! However, somethings that work perfectly in .NET cannot be ported to PHP, at least as a PHP written library.

There is a Java implementation of LINQ, for instance, that uses strings as well, so it's only about the fact whether the language is loosely-typed or not.


Aside from that, I never got what the fuss over Linq is all about. If you look at it closely, it almost looks as if you're writing SQL anyway - just that you have to re-learn the syntax for the slight differences, have less readable code, have fewer possibilities for optimizing. For what benefit? Portability? If I cared about that I'd use a proper ORM and not care about raw SQL queries anyway, thank you very much. Linq is another good example for a solution looking for a problem.



Aside from that, I never got what the fuss over Linq is all about. If you look at it closely, it almost looks as if you're writing SQL anyway - just that you have to re-learn the syntax for the slight differences, have less readable code, have fewer possibilities for optimizing. For what benefit? Portability? If I cared about that I'd use a proper ORM and not care about raw SQL queries anyway, thank you very much. Linq is another good example for a solution looking for a problem.


For what benefit? For efficiency! LINQ does really helps you to solve your problems much faster than you were doing it in the .NET 2.0 style. If you need an ORM, you can use built-in LINQ to SQL or Entity Framework that will be shipped with VS 2008 SP1, you don't have to use any 3rd party tools, you don't have to write your own ORM. It's already there.

Jonathan Bond-Caron - Ideas & Experiments

Thursday, July 24, 2008 12:18 PM GMT

trackback

Trackback from Jonathan Bond-Caron - Ideas & Experiments

PHP LINQ?

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]
Loading