mirror of
https://github.com/php/doc-ru.git
synced 2026-03-26 00:32:15 +01:00
146 lines
3.5 KiB
XML
146 lines
3.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- EN-Revision: 914b97130aed191518791045b93b6f858ef5a139 Maintainer: rjhdby Status: ready -->
|
||
<!-- Reviewed: no -->
|
||
<chapter xml:id="yar.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||
&reftitle.examples;
|
||
<example>
|
||
<title>Пример сервера Yar</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
/* Предположим, что это страница может быть доступна по http://example.com/operator.php */
|
||
|
||
class Operator {
|
||
|
||
/**
|
||
* Складываем два операнда
|
||
* @param interge
|
||
* @return interge
|
||
*/
|
||
public function add($a, $b) {
|
||
return $this->_add($a, $b);
|
||
}
|
||
|
||
/**
|
||
* Вычитаем
|
||
*/
|
||
public function sub($a, $b) {
|
||
return $a - $b;
|
||
}
|
||
|
||
/**
|
||
* Умножаем
|
||
*/
|
||
public function mul($a, $b) {
|
||
return $a * $b;
|
||
}
|
||
|
||
/**
|
||
* Защищённый метод
|
||
* @param interge
|
||
* @return interge
|
||
*/
|
||
protected function _add($a, $b) {
|
||
return $a + $b;
|
||
}
|
||
}
|
||
|
||
$server = new Yar_Server(new Operator());
|
||
$server->handle();
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
<example>
|
||
<title>Обращаемся к серверу из браузера (запрос GET)</title>
|
||
&example.outputs.similar;
|
||
<mediaobject>
|
||
<alt>Информация о сервере Yar</alt>
|
||
<imageobject>
|
||
<imagedata fileref="en/reference/yar/image/yar.png" scalefit="1" width="700px" />
|
||
</imageobject>
|
||
</mediaobject>
|
||
</example>
|
||
|
||
<example>
|
||
<title>Пример клиента Yar</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$client = new yar_client("http://example.com/operator.php");
|
||
|
||
/* вызываем напрямую */
|
||
var_dump($client->add(1, 2));
|
||
|
||
/* вызываем через метод call */
|
||
var_dump($client->call("add", array(3, 2)));
|
||
|
||
|
||
/* невозможно вызвать __add */
|
||
var_dump($client->_add(1, 2));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.similar;
|
||
<screen>
|
||
<![CDATA[
|
||
int(3)
|
||
int(5)
|
||
PHP Fatal error: Uncaught exception 'Yar_Server_Exception' with message 'call to api Operator::_add() failed' in *
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<example>
|
||
<title>Пример конкурирующих клиентов Yar</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?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_Concurrent_Client::loop();
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.similar;
|
||
<screen>
|
||
<![CDATA[
|
||
mul result: 4
|
||
sub result: 1
|
||
add result: 3
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
</chapter>
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|