codeplex:PHP LINQ classes,PHP也可以使用Linq语法操作对象

来源:互联网 发布:尔雅网络课程登录入口 编辑:程序博客网 时间:2024/05/16 09:24

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. (待续...)

转载地址:http://www.weiqinxue.cn/blogs/index.php/User/articleview/ArticleID/U2A15

0 0
原创粉丝点击