• Start

Languages

/

PHP

/

v2 (alpha)

/

API Reference

Core classes

Reference for the Surreal class, the ConnectionController, and the supporting connection types in version 2 of the PHP SDK.

The Surreal class is the entry point for version 2 of the SDK. It manages the connection, the session, authentication, and query execution.

Namespace: SurrealDB\SDK\Surreal

Source: src/Surreal.php

Syntax

new Surreal(?DriverOptions $options = null)

DriverOptions customises driver-wide behaviour such as the codec, HTTP client, scheduler, and middleware. Pass null for the defaults.

use SurrealDB\SDK\Surreal;

$db = new Surreal();

Connect to a SurrealDB endpoint.

Syntax

$db->connect(string|Endpoint $url, ?ConnectOptions $options = null): void
ParameterTypeDescription
url string | EndpointThe endpoint to connect to, such as ws://127.0.0.1:8000/rpc.
options ConnectOptionsNamespace, database, authentication, and reconnection settings.
$db->connect('ws://127.0.0.1:8000/rpc', new ConnectOptions(
namespace: 'surrealdb',
database: 'docs',
));

Close the connection and release its resources.

Syntax

$db->close(): void

Return the current ConnectionStatus.

Syntax

$db->status(): ConnectionStatus

Return whether the connection is established. Equivalent to status() === ConnectionStatus::Connected.

Syntax

$db->isConnected(): bool

Throw if the instance is unreachable, otherwise return nothing.

Syntax

$db->health(): void

Return the server version string, for example surrealdb-2.1.0.

Syntax

$db->version(): string

Return whether a feature is available on the current connection and server.

Syntax

$db->isFeatureSupported(Feature $feature): bool

Subscribe to a lifecycle event: connecting, connected, reconnecting, disconnected, error, auth, or using. Returns a closure that removes the listener.

Syntax

$db->subscribe(string $event, callable $listener): Closure

Return the underlying ConnectionController for advanced operations such as transactions, sessions, and import/export.

Syntax

$db->connection(): ConnectionController

Select a namespace and an optional database.

Syntax

$db->use(?string $namespace, ?string $database = null): void

Define a session parameter, available in later queries as $name.

Syntax

$db->let(string $name, mixed $value): void

Remove a session parameter.

Syntax

$db->unset(string $name): void

Sign in with a credential object or an array. Returns a Tokens object.

Syntax

$db->signin(Credentials|array $auth): Tokens

Sign up a new record user. Returns a Tokens object.

Syntax

$db->signup(Credentials|array $auth): Tokens

Authenticate the session with an existing token.

Syntax

$db->authenticate(Token|string $token): void

Clear the session's authentication.

Syntax

$db->invalidate(): void

Execute a BoundQuery, returning one result per statement.

Syntax

$db->query(BoundQuery $query): array

Execute raw SurrealQL with optional bindings, returning one result per statement.

Syntax

$db->run(string $surql, array $bindings = []): array

These methods start a fluent query builder. Call execute() to run it.

  • select($what), create($what), update($what), upsert($what), delete($what)

  • insert($tableOrData, $data = null), relate($from, $edge, $to, $data = null)

  • call($name, $version = null, $args = []), auth()

Subscribe to a live query by its ID. Returns an iterable of LiveMessage objects.

Syntax

$db->live(string $queryUuid): iterable

Iterating the result blocks the process while it waits for messages. See running without blocking the application for consuming live queries in a worker.

Supporting types

Per-connection settings passed to connect().

Namespace: SurrealDB\SDK\Connection\ConnectOptions

Constructor

new ConnectOptions(
?string $namespace = null,
?string $database = null,
Credentials|Token|AuthProviderInterface|Closure|string|null $authentication = null,
bool $versionCheck = true,
bool $invalidateOnExpiry = false,
bool|array|ReconnectStrategyInterface $reconnect = true,
)

A string-backed enum with the connection lifecycle states.

Namespace: SurrealDB\SDK\Connection\ConnectionStatus

Values: Disconnected, Connecting, Reconnecting, Connected

The result of signin() and signup(): an access token and an optional refresh token.

Namespace: SurrealDB\SDK\Auth\Tokens

Properties

$tokens->access;   // ?string
$tokens->refresh; // ?string

A single live query notification.

Namespace: SurrealDB\SDK\Live\LiveMessage

Properties

$message->queryId;  // string
$message->action; // LiveAction (Create, Update, Delete, Killed)
$message->record; // mixed: the affected record id
$message->value; // mixed: the new record value

ConnectionController

The controller orchestrates the connection. Access it with $db->connection() for operations that are not on the Surreal facade.

Namespace: SurrealDB\SDK\Connection\ConnectionController

Syntax

$db->connection()->begin(?string $session = null): string
$db->connection()->commit(string $txn, ?string $session = null): void
$db->connection()->cancel(string $txn, ?string $session = null): void

Syntax

$db->connection()->importSql(string $data): void
$db->connection()->exportSql(array $options = []): string
$sql = $db->connection()->exportSql();
$db->connection()->importSql($sql);

Syntax

$db->connection()->refresh(Tokens $tokens, ?string $session = null): Tokens
$db->connection()->revoke(Tokens $tokens, ?string $session = null): void

Syntax

$db->connection()->sessions(): array
$db->connection()->createSession(?string $clone = null): string
$db->connection()->destroySession(?string $session): void

Was this page helpful?