php8.5
Home/ Manual/ yar / yar_server/ Yar_Server::__construct

Yar_Server::__construct

PHP function Edit on GitHub ✎

(PECL yar >= 1.0.0)

Create an RPC server

Description

Yar_Server::__construct(object $executor)

Creates a Yar HTTP RPC server. All public methods of executor are registered as RPC services.

Parameters

executor

Any object whose public methods should be exposed as RPC services. Protected and private methods, and methods whose name starts with an underscore, are not exposed.

The executor can optionally define two protected magic methods, which are recognized by Yar_Server::handle and never exposed as RPC endpoints:

  • protected function __info(string $markup): string — as of Yar 2.3.0. Called when the service information page is requested; it receives the markup of the page Yar would otherwise render, and if it returns a String, that string is sent to the client instead. See Yar_Server::handle.
  • protected function __auth(string $provider, string $token): bool — as of Yar 2.3.0. Called at the very beginning of every request with the provider and token fields of the request header, set by the client with the YAR_OPT_PROVIDER and YAR_OPT_TOKEN options. Returning exactly false rejects the request; any other return value lets it proceed. See Yar_Server::handle.

Both methods only take effect when they are protected; a public or private method with the same name is ignored.

Return Values

An instance of Yar_Server.

Examples

Yar_Server::__construct example

php
<?php
class API {
    /**
     * The doc block is shown on the service info page.
     * @param string $parameter
     * @return string
     */
    public function some_method($parameter, $option = "foo") {
         return "some_method";
    }

    protected function client_can_not_see() {
    }
}

$service = new Yar_Server(new API());
$service->handle();
?>

See Also

  • Yar_Server::handle

Source: reference/yar/yar_server/construct.xml · from the official PHP manual (php/doc-en)