Example #1 Yar Server示锟斤拷
<?php
/* 锟斤拷锟斤拷锟斤拷锟揭筹拷锟侥凤拷锟斤拷路锟斤拷锟斤拷: http://example.com/operator.php */
class Operator {
/**
* Add two operands
* @param interge
* @return interge
*/
public function add($a, $b) {
return $this->_add($a, $b);
}
/**
* Sub
*/
public function sub($a, $b) {
return $a - $b;
}
/**
* Mul
*/
public function mul($a, $b) {
return $a * $b;
}
/**
* Protected methods will not be exposed
* @param interge
* @return interge
*/
protected function _add($a, $b) {
return $a + $b;
}
}
$server = new Yar_Server(new Operator());
$server->handle();
?>
Example #2 通锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�(GET锟斤拷锟斤拷)
锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�
Example #3 Yar Client示锟斤拷
<?php
$client = new yar_client("http://example.com/operator.php");
/* call directly */
var_dump($client->add(1, 2));
/* call via call */
var_dump($client->call("add", array(3, 2)));
/* __add can not be called */
var_dump($client->_add(1, 2));
?>
锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�
int(3) int(5) PHP Fatal error: Uncaught exception 'Yar_Server_Exception' with message 'call to api Operator::_add() failed' in *
Example #4 Yar Concurrent Client示锟斤拷
<?php
function callback($ret, $callinfo) {
echo $callinfo['method'] , " result: ", $ret , "\n";
}
/* 注锟斤拷一锟斤拷锟届步锟斤拷锟斤拷 */
Yar_Concurrent_Client::call("http://example.com/operator.php", "add", array(1, 2), "callback");
Yar_Concurrent_Client::call("http://example.com/operator.php", "sub", array(2, 1), "callback");
Yar_Concurrent_Client::call("http://example.com/operator.php", "mul", array(2, 2), "callback");
/* 锟斤拷锟斤拷锟斤拷锟斤拷注锟斤拷牡锟斤拷锟�, 锟饺达拷锟斤拷锟斤拷, 锟斤拷锟截猴拷Yar锟斤拷锟斤拷锟絚allback锟截碉拷锟斤拷锟斤拷 */
Yar_Concurrent_Client::loop();
?>
锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�
mul result: 4 sub result: 1 add result: 3