There are two projects that I've been following for awhile now: PLINQ
and PHPLinq.
 http://plinq.codeplex.com/
 http://phplinq.codeplex.com/
 Both of them have made very solid attempts at providing LINQ-like
functionality to PHP but with both, I've been a little frustrated
with the implementations, due to the wordy syntax that PHP lambda
functions require.
 LINQ operates by the use of predicates: a function that will be run
against each item in whatever datasource you happen to be querying
and will resolve to true or false - literally, whether the current
item should be included in the final result set. Both PLINQ and
PHPLinq have achieved roughly the same level of technology. However,
PHPLinq makes use of runtime evaluated strings to mimic the C#
implementation and PLINQ makes use of PHP's existing lambda style,
which can be very wordy. 
 Here is an example of both.
 PHPLinq
        // Create data source $names = array("John", "Peter", "Joe",
"Patrick", "Donald", "Eric");  $result = from('$name')->in($names)   
         ->where('$name => strlen($name) < 5')            
->select('$name');
 Notice the quoted strings around the "where" clause in that
statement. The reason for this was to provide as close as syntax to
C# as possible (PHPLinq was created by a Microsoft MVP, if I remember
correctly). The syntax is definitely shorter than anything PHP could
currently provide, but it requires string evaluation and comes with
all of the many downsides of being string evaluated that I'm sure are
already rifling through your head right now.
 Now look at PLINQ.
        $plinq = new Plinq($users);
 //gets the users that are older than 60
 $olderUsers = $plinq->Where(function($k, $v){ return $v->Age > 60;
});PLINQ definitely follows the "correct" way handling predicated
statements. I'm curious though, what people thought about providing a
simpler, shorthand way of providing lambda functions (this isn't a
request for LINQ in PHP! I'm merely using it as an example showing
the extremes in PHP right now).
 Rewriting the statement above could look like this:
 $olderUsers = $plinq->Where($v => $v->Age > 60); //this is just
suggestive syntax
 This eliminates a swath of unnecessary text - specifically, the
"function" and "return" keywords and the associated brackets and
parentheses - the result is less typing and a more readable block of
code. I love closures in PHP but often I find myself in a situation
like the one above - I need a quick function that I will never use
again and it would be great if there was a shorthand for creating
such simple statements.
 What does everyone else think?
 

Reply via email to