Click here to Skip to main content
12,245,023 members (51,127 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6K views
3 bookmarked
Posted

LINQ for PHP

, 5 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
The driving forces behind LINQ for C# are extension methods. Without extension methods, LINQ becomes a cumbersome, syntax-heavy burden. It's this burden that I've translated to PHP.Below, you'll find a few LINQ-like methods for PHP. Each of the methods takes two arguments: a collection and a predica

The driving forces behind LINQ for C# are extension methods. Without extension methods, LINQ becomes a cumbersome, syntax-heavy burden. It's this burden that I've translated to PHP.

Below, you'll find a few LINQ-like methods for PHP. Each of the methods takes two arguments: a collection and a predicate. The method simply iterates through the collection, tests the predicate against each item, and returns different results depending on the purpose of the call.

I've mimicked the following LINQ methods from C#:

  • Count
  • Where
  • Select
  • SelectMany
  • First
  • FirstOrDefault
  • Last
  • LastOrDefault

It's good to note that, not only is this manner of incorporating predicates compatible with PHP 5.2, it also doesn't rely on string parsing to create a lamda expression. The predicate is created with good-ol' create_function.

If you're looking for a full-fledged LINQ solution for PHP, check out PHPLinq.

As an added bonus, there's a simple yet effective test framework included in the example below. I'll try to write more about it some other time, but, for now, you should see the following result if you run the script in a browser:

Starting tests...
15 tests found.
Performing testAssertFailed...
Performing testAssertAreEqual...
Performing testAssertAreEqual2...
Performing testAssertAreNotEqual...
Performing testAssertAreNotEqual2...
Performing testAssertIsTrue...
Performing testAssertIsFalse...
Performing testCmGnWhere...
Performing testCmGnCount...
Performing testCmGnSelect...
Performing testCmGnSelectMany...
Performing testCmGnFirst...
Performing testCmGnFirstOrNull...
Performing testCmGnLast...
Performing testCmGnLastOrNull...
15 of 15 tests passed.

Stay tuned for the upcoming phpChimpanzee framework which contains more ugly solutions to everyday coding problems.

<?php
 
	class cmGn {
		public static function <a href="http://www.php.net/count">count</a>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

qenn
Engineer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
ProgramFOX28-Jul-13 5:19
member ProgramFOX28-Jul-13 5:19 
GeneralMy vote of 1 Pin
stikves8-Jan-12 19:55
memberstikves8-Jan-12 19:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 5 Jan 2012
Article Copyright 2012 by qenn
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid