Skip to content

Commit 92d3208

Browse files
committed
Add documentation for magic __call() method
1 parent cc0f4ac commit 92d3208

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

README.md

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ It enables you to set and query its data or use its PubSub topics to react to in
3737
* [createClient()](#createclient)
3838
* [createLazyClient()](#createlazyclient)
3939
* [Client](#client)
40+
* [__call()](#__call)
4041
* [end()](#end)
4142
* [close()](#close)
4243
* [error event](#error-event)
@@ -86,7 +87,8 @@ See also the [examples](examples).
8687

8788
### Commands
8889

89-
All [Redis commands](https://redis.io/commands) are automatically available as public methods like this:
90+
Most importantly, this project provides a [`Client`](#client) instance that
91+
can be used to invoke all [Redis commands](https://redis.io/commands) (such as `GET`, `SET`, etc.).
9092

9193
```php
9294
$client->get($key);
@@ -107,26 +109,41 @@ $client->select($database);
107109
// many more…
108110
```
109111

110-
Listing all available commands is out of scope here, please refer to the [Redis command reference](https://redis.io/commands).
111-
All [Redis commands](https://redis.io/commands) are automatically available as public methods via the magic `__call()` method.
112+
Each method call matches the respective [Redis command](https://redis.io/commands).
113+
For example, the `$redis->get()` method will invoke the [`GET` command](https://redis.io/commands/get).
112114

113-
Each of these commands supports async operation and either *resolves* with
114-
its *results* or *rejects* with an `Exception`.
115-
Please see the following section about [promises](#promises) for more details.
115+
All [Redis commands](https://redis.io/commands) are automatically available as
116+
public methods via the magic [`__call()` method](#__call).
117+
Listing all available commands is out of scope here, please refer to the
118+
[Redis command reference](https://redis.io/commands).
119+
120+
Any arguments passed to the method call will be forwarded as command arguments.
121+
For example, the `$redis->set('name', 'Alice')` call will perform the equivalent of a
122+
`SET name Alice` command. It's safe to pass integer arguments where applicable (for
123+
example `$redis->expire($key, 60)`), but internally Redis requires all arguments to
124+
always be coerced to string values.
125+
126+
Each of these commands supports async operation and returns a [Promise](#promises)
127+
that eventually *fulfills* with its *results* on success or *rejects* with an
128+
`Exception` on error. See also the following section about [promises](#promises)
129+
for more details.
116130

117131
### Promises
118132

119-
Sending commands is async (non-blocking), so you can actually send multiple commands in parallel.
120-
Redis will respond to each command request with a response message, pending commands will be pipelined automatically.
133+
Sending commands is async (non-blocking), so you can actually send multiple
134+
commands in parallel.
135+
Redis will respond to each command request with a response message, pending
136+
commands will be pipelined automatically.
121137

122-
Sending commands uses a [Promise](https://github.com/reactphp/promise)-based interface that makes it easy to react to when a command is *fulfilled*
123-
(i.e. either successfully resolved or rejected with an error):
138+
Sending commands uses a [Promise](https://github.com/reactphp/promise)-based
139+
interface that makes it easy to react to when a command is completed
140+
(i.e. either successfully fulfilled or rejected with an error):
124141

125142
```php
126-
$client->set('hello', 'world');
127-
$client->get('hello')->then(function ($response) {
128-
// response received for GET command
129-
echo 'hello ' . $response;
143+
$redis->get($key)->then(function (string $value) {
144+
var_dump($value);
145+
}, function (Exception $e) {
146+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
130147
});
131148
```
132149

@@ -510,6 +527,38 @@ and keeps track of pending commands.
510527
Besides defining a few methods, this interface also implements the
511528
`EventEmitterInterface` which allows you to react to certain events as documented below.
512529

530+
#### __call()
531+
532+
The `__call(string $name, string[] $args): PromiseInterface<mixed,Exception>` method can be used to
533+
invoke the given command.
534+
535+
This is a magic method that will be invoked when calling any Redis command on this instance.
536+
Each method call matches the respective [Redis command](https://redis.io/commands).
537+
For example, the `$redis->get()` method will invoke the [`GET` command](https://redis.io/commands/get).
538+
539+
```php
540+
$redis->get($key)->then(function (string $value) {
541+
var_dump($value);
542+
}, function (Exception $e) {
543+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
544+
});
545+
```
546+
547+
All [Redis commands](https://redis.io/commands) are automatically available as
548+
public methods via this magic `__call()` method.
549+
Listing all available commands is out of scope here, please refer to the
550+
[Redis command reference](https://redis.io/commands).
551+
552+
Any arguments passed to the method call will be forwarded as command arguments.
553+
For example, the `$redis->set('name', 'Alice')` call will perform the equivalent of a
554+
`SET name Alice` command. It's safe to pass integer arguments where applicable (for
555+
example `$redis->expire($key, 60)`), but internally Redis requires all arguments to
556+
always be coerced to string values.
557+
558+
Each of these commands supports async operation and returns a [Promise](#promises)
559+
that eventually *fulfills* with its *results* on success or *rejects* with an
560+
`Exception` on error. See also [promises](#promises) for more details.
561+
513562
#### end()
514563

515564
The `end():void` method can be used to

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
interface Client extends EventEmitterInterface
2323
{
2424
/**
25-
* Invoke the given command and return a Promise that will be resolved when the request has been replied to
25+
* Invoke the given command and return a Promise that will be fulfilled when the request has been replied to
2626
*
2727
* This is a magic method that will be invoked when calling any redis
2828
* command on this instance.

0 commit comments

Comments
 (0)