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:
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;
//create xmlrpcval object, which allows the encoding of our variable //创建xmlrpcval对象,将我们的PHP变量编码为XML-RPC需要的XML形式 #$inputString=new xmlrpcval(''world胜利'', ''string''); $inputString = php_xmlrpc_encode(''胜利world'');
//create an array of parameters //尽管我们只有一个参数,但仍然要转换成数组的形式,因为xmlrpcmsg的第二个参数是一个参数表 $parameters=array($inputString);
//create the message object //创建XML-RPC报文,参数分别为 远程方法名 和 参数表 $msg=new xmlrpcmsg(''echoString'', $parameters); //$s->request_charset_encoding = ''utf-8''; //send the message, get the response //发送报文,返回值$rsp为一个xmlrpcresp对象,它包含以下三个方法: //faultCode() 出错代码,如果成功将返回0 //faultString() 出错信息 //value() 返回值,以xmlrpcval对象形式存在,PHP使用前需要进行解码 $s->debug = true; $rsp=$s->send($msg);
//check for errors if($rsp->faultcode()==0) { //decode the response to a PHP type //xmlrpc_decode()函数用于将xmlrpcval对象解码 $response=php_xmlrpc_decode($rsp->value());