On Sun, May 29, 2011 at 01:59:45PM -0700, elij wrote:
On Sun, May 29, 2011 at 7:27 AM, Lukas Fleischer <archlinux@cryptocrack.de> wrote:
On Sat, May 28, 2011 at 04:17:09PM -0700, elij wrote:
+ if(EXTENSION_LOADED_APC) { + $ret = apc_fetch(APC_PREFIX.$key, $status); + if ($status) { + return $ret; + } + } + return $status; +}
I'd prefer to change get_cache_value()'s signature to return the status indicator and pass the actual result by reference. That way, it could be used as follows:
---- if (get_cache_value('foo', $foo)) { do_something $foo } ----
That just feels much more common and convenient. Any objections?
That seems a bit unconventional, considering the existing codebase and common php practices. While it might make for the occasional nice conditional-if test, I think it is more of a leaky abstraction than the existing method.
[...]
Memcache::get returns false on failure, which can be problematic if a falsey value was stored in memcache, and you were trying to get it out (and test that retrieval succeeded). Passing a bool by reference fixes that case.
You _can_ use a convention like this:
if(!($foo = get_cache_value('bar'))) { // do stuff }
As get_cache_value on failure *also* returns a falsey value, but this runs into the memcache api problem of what happens if you want to retrieve a falsey value. If you know that either you never store a falsey value for that key, or if your conditional test is appropriate assuming a falsey value stored (eg. if the condition is false due to failure or retrieval of a falseyness, the expected behavior is the same), then that convention works fine.
Yes, I was aware of that.
Passing a data container by reference (your suggestion) would also work fine, but I don't see that very often in practice. I am probably not very current on php conventions though, and I am using the php documentation as a reference for 'best practices for api signatures'. Which may be a fools errand to some extent. ;)
Do you make use of the 'pass data container by reference' convention regularly, or see it commonly used?
Well, I can't think of some good examples right now but it feels more common and convenient to me. preg_match() and "&$matches" is kinda similar but different, still. It actually doesn't make all the difference here, so let's just keep its signature for now. It can still be changed in a future patch. Thanks!