This project is read-only.
Home Features Requirements Examples FAQ Contribute Credits Contact

Examples

Examples can be found in the test package in the latest release.

A basic example

Let's say we have an array of strings and want to select only the strings whose length is < 5. The PHPLinq way of achieving this would be the following:
// Create data source
$names = array("John", "Peter", "Joe", "Patrick", "Donald", "Eric"); 

$result = from('$name')->in($names)
            ->where('$name => strlen($name) < 5')
            ->select('$name'); 


Feels familiar to SQL? Yes indeed! No more writing a loop over this array, checking the string's length, and adding it to a temporary variable.

You may have noticed something strange... What's that $name => strlen($name) < 5 doing? This piece of code is compiled to an anonymous function or Lambda expression under the covers. This function accepts a parameter $name, and returns a boolean value based on the expression strlen($name) < 5.

An advanced example

There are lots of other examples available in the PHPLinq download, but here's an advanced one... Let's say we have an array of Employee objects. This array should be sorted by Employee name, then Employee age. We want only Employees whose name has a length of 4 characters. Next thing: we do not want an Employee instance in our result. Instead, the returning array should contain objects containing an e-mail address and a domain name.

First of all, let's define our data source:
class Employee {
    public $Name;
    public $Email;
    public $Age;

    public function __construct($name, $email, $age) {
        $this->Name     = $name;
        $this->Email     = $email;
        $this->Age        = $age;
    }
} 

$employees = array(
    new Employee('Maarten', 'maarten@example.com', 24),
    new Employee('Paul', 'paul@example.com', 30),
    new Employee('Bill', 'bill.a@example.com', 29),
    new Employee('Bill', 'bill.g@example.com', 28),
    new Employee('Xavier', 'xavier@example.com', 40)
); 


Now for the PHPLinq query:
$result = from('$employee')->in($employees)
            ->where('$employee => strlen($employee->Name) == 4')
            ->orderBy('$employee => $employee->Name')
            ->thenByDescending('$employee => $employee->Age')
            ->select('new {
                    "EmailAddress" => $employee->Email,
                    "Domain" => substr($employee->Email, strpos($employee->Email, "@") + 1)
                  }'); 


Again, you may have noticed something strange... What's this new { } thing doing? Actually, this is converted to an anonymous type under the covers. new { "name" => "test" } is evaluated to an object containing the property "name" with a value of "test".

Last edited Jan 22, 2008 at 1:55 PM by maartenba, version 4

Comments

likui0623 Jan 15, 2014 at 7:51 AM 
That is a very graceful way of coding. It looks like 'linq to object' in .NET framework platform.

kam03m Dec 12, 2013 at 10:47 PM 
I'm not sure about this syntax. Defining code as strings is kind of rough. I guess that php isn't really very mature when it comes to passing anonymous functions around?

flyingchen Jul 16, 2010 at 8:30 AM 
how about the performance?

hamada6004 Jun 22, 2010 at 9:27 AM 
ok thank you i Know now

hamada6004 Jun 22, 2010 at 9:10 AM 
this code where i use it . i mean any Environment programming i must use. are this php code .
write it like any code in my editor.
. Maybe

maartenba Nov 3, 2009 at 7:43 PM 
Have not benchmarked...

hlaue Nov 3, 2009 at 4:45 PM 
I wonder how much overhead is involved when using PHPLinq. Don't get me wrong, it's great to see, but I'm the curious type when it comes to how preformance (memory/speed/disk/etc) is affected.

hlaue Nov 3, 2009 at 4:45 PM 
I wonder how much overhead is involved when using PHPLinq. Don't get me wrong, it's great to see, but I'm the curious type when it comes to how preformance (memory/speed/disk/etc) is effected.

leblanc Nov 6, 2008 at 1:07 AM 
wow i haven't programmed in php in about 3 years...
use to hang out at sitepoint many years ago...
glad to know php is staying with the changing times.