Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Nearly all the methods in redis.stub.php include Redis in their union return type, but I've scoured the documentation and this doesn't seem to be mentioned anywhere. Am I just blind? In what situations will the various Redis methods return a Redis instance? And does it return $this, or could it potentially return a completely different instance? Why the ambiguity? I see there are several issues and PRs dealing with return types, but none are addressing this issue in particular.

It would be great if 7.0 could remove this possibility entirely, as well as returning false for errors instead of throwing an exception. That seems like a really outdated anti-pattern. These two things make for a pretty awful developer experience. Having to constantly double-check the return type of Every. Single. Method. is quite tedious.

You must be logged in to vote

As @yatsukhnenko posted it's when you are in a transaction (either MULTI, PIPELINE, or both). This allows you to chain commands

$redis = new Redis;
$redis->connect('localhost', 6379);

$redis->multi()               // Returns Redis ($this)
    ->set('user:id', 1)       // Returns Redis ($this)
    ->set('user:name', 'Bob') // Returns Redis ($this)
    ->exec();                 // Returns array of results: [true, true]

And on the server:

1778176160.762464 [0 [::1]:45330] "MULTI"
1778176160.762775 [0 [::1]:45330] "SET" "user:id" "1"
1778176160.762789 [0 [::1]:45330] "SET" "user:name" "Bob"
1778176160.762790 [0 [::1]:45330] "EXEC"

We only ever semantically return $this never an unrelated Redis

Replies: 1 comment · 2 replies

Comment options

@hackel https://github.com/phpredis/phpredis#transactions

You must be logged in to vote
2 replies
@michael-grunder
Comment options

As @yatsukhnenko posted it's when you are in a transaction (either MULTI, PIPELINE, or both). This allows you to chain commands

$redis = new Redis;
$redis->connect('localhost', 6379);

$redis->multi()               // Returns Redis ($this)
    ->set('user:id', 1)       // Returns Redis ($this)
    ->set('user:name', 'Bob') // Returns Redis ($this)
    ->exec();                 // Returns array of results: [true, true]

And on the server:

1778176160.762464 [0 [::1]:45330] "MULTI"
1778176160.762775 [0 [::1]:45330] "SET" "user:id" "1"
1778176160.762789 [0 [::1]:45330] "SET" "user:name" "Bob"
1778176160.762790 [0 [::1]:45330] "EXEC"

We only ever semantically return $this never an unrelated Redis object.

Answer selected by hackel
@hackel
Comment options

Aha! Glad it was in fact just me being blind. Thank you both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.