DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. PHPLinq As Cool As Real LINQ?

PHPLinq As Cool As Real LINQ?

Mike Borozdin user avatar by
Mike Borozdin
·
Jul. 10, 08 · News
Like (0)
Save
Tweet
Share
13.09K Views

Join the DZone community and get the full member experience.

Join For Free

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 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);

What do you think?

Database

Published at DZone with permission of Mike Borozdin. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Beginners’ Guide to Run a Linux Server Securely
  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!
  • Cloud-Based Transportation Management System
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: