Skip to content

Commit c9e4787

Browse files
committed
Fix null handling for PHP 8
1 parent bf8e500 commit c9e4787

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

object-cache.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,25 @@ function get( $id, $group = 'default', $force = false, &$found = null ) {
489489
if ( false === $flags ) {
490490
$found = false;
491491
$value = false;
492+
} elseif ( false === $value && ( $flags & 0xFF01 ) === 0x01 ) {
493+
/*
494+
* The lowest byte is used for flags.
495+
* 0x01 means the value is serialized (MMC_SERIALIZED).
496+
* The second lowest indicates data type: 0 is string, 1 is bool, 3 is long, 7 is double.
497+
* `null` is serialized into a string, thus $flags is 0x0001
498+
* Empty string will correspond to $flags = 0x0000 (not serialized).
499+
* For `false` or `true` $flags will be 0x0100
500+
*
501+
* See:
502+
* - https://github.com/websupport-sk/pecl-memcache/blob/2a5de3c5d9c0bd0acbcf7e6e0b7570f15f89f55b/php7/memcache_pool.h#L61-L76 - PHP 7.x
503+
* - https://github.com/websupport-sk/pecl-memcache/blob/ccf702b14b18fce18a1863e115a7b4c964df952e/src/memcache_pool.h#L57-L76 - PHP 8.x
504+
*
505+
* In PHP 8, they changed the way memcache_get() handles `null`:
506+
* https://github.com/websupport-sk/pecl-memcache/blob/ccf702b14b18fce18a1863e115a7b4c964df952e/src/memcache.c#L2175-L2177
507+
*
508+
* If the return value is `null`, it is silently converted to `false`. We can only rely upon $flags to find out whther `false` is real.
509+
*/
510+
$value = null;
492511
}
493512

494513
$this->cache[ $key ] = [

0 commit comments

Comments
 (0)