Hacker News new | ask | show | jobs
by tyingq 1748 days ago
On the PHP api, you're returning 0 when there's a failure. You also return 0 when there is no such key on a get().

That isn't a great idea, since that would mean no discernable difference from

- An error, like a network error

- No such key exists

- Key did exist, and it's value was 0, though I suppose I could test the type with gettype()? Seems odd to return an int instead of a boolean though.

For comparison, the phpredis api raises an exception for errors and returns a boolean false if no such key exists.

1 comments

This is great feedback, yingq. Thanks for it.

I'll research a bit on providing a different approach. I am now thinking about returning arrays (containing a $status var), but will remain open to ideas.

Ideas? At any rate, I appreciate your input.

Both the redis and memcached PHP apis return boolean false when their equivalent of get() is called and no such key exists.

Personally, I'd stick with that and raise an exception on network and similar types of errors. PHP also supports trigger_error() but there doesn't seem to be top level guidance on errors vs exceptions. Try/catch just seems more straightforward to me.

This is exactly what I had in mind. I'll stick with it and add a catch exception.

Thanks for your feedback!