From 1164100b582eeb49802aa1b24ebf1587e9eb5736 Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Sun, 29 Nov 2026 20:59:46 +0100 Subject: [PATCH] Fix PHP 8.6 deprecation warnings for usage of spl_object_hash The deprecation warning is "Function spl_object_hash() is deprecated since 8.6, consider using spl_object_id() instead" See https://www.php.net/releases/8.6/en.php#deprecations-and-bc-breaks Closes #931 --- lib/Phpfastcache/CacheManager.php | 2 +- lib/Phpfastcache/Cluster/ClusterAggregator.php | 6 +++--- lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php | 8 ++++---- lib/Phpfastcache/EventManager.php | 2 +- tests/cases/CacheClearSingleInstance.test.php | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/Phpfastcache/CacheManager.php b/lib/Phpfastcache/CacheManager.php index 842fa5ab..c335c635 100644 --- a/lib/Phpfastcache/CacheManager.php +++ b/lib/Phpfastcache/CacheManager.php @@ -247,7 +247,7 @@ public static function clearInstance(ExtendedCacheItemPoolInterface $cachePoolIn self::$instances = \array_filter( \array_map( static function (ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInstance, &$found) { - if (\spl_object_hash($cachePool) === \spl_object_hash($cachePoolInstance)) { + if (\spl_object_id($cachePool) === \spl_object_id($cachePoolInstance)) { $found = true; return null; } diff --git a/lib/Phpfastcache/Cluster/ClusterAggregator.php b/lib/Phpfastcache/Cluster/ClusterAggregator.php index 69df214f..3955f09b 100644 --- a/lib/Phpfastcache/Cluster/ClusterAggregator.php +++ b/lib/Phpfastcache/Cluster/ClusterAggregator.php @@ -52,7 +52,7 @@ public function __construct(string $clusterAggregatorName = '', AggregatablePool try { $clusterAggregatorName = 'cluster_' . \bin2hex(\random_bytes(15)); } catch (Exception) { - $clusterAggregatorName = 'cluster_' . \str_shuffle(\spl_object_hash(new stdClass())); + $clusterAggregatorName = 'cluster_' . \str_shuffle(\spl_object_id(new stdClass())); } } @@ -74,7 +74,7 @@ public function aggregateDriver(AggregatablePoolInterface $driverPool): void throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.'); } - $splHash = \spl_object_hash($driverPool); + $splHash = \spl_object_id($driverPool); if (!isset($this->driverPools[$splHash])) { if ($driverPool instanceof ClusterPoolInterface) { throw new PhpfastcacheLogicException('Recursive cluster aggregation is not allowed !'); @@ -126,7 +126,7 @@ public function disaggregateDriver(AggregatablePoolInterface $driverPool): void throw new PhpfastcacheLogicException('The cluster has been already build, cannot disaggregate pools.'); } - $splHash = \spl_object_hash($driverPool); + $splHash = \spl_object_id($driverPool); if (isset($this->driverPools[$splHash])) { unset($this->driverPools[$splHash]); } else { diff --git a/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php b/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php index a66e99c1..b1ad32d7 100644 --- a/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php +++ b/lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php @@ -398,7 +398,7 @@ public function saveDeferred(CacheItemInterface $item): bool $this->assertCacheItemType($item, self::getItemClass()); if (!\array_key_exists($item->getKey(), $this->itemInstances)) { $this->itemInstances[$item->getKey()] = $item; - } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { + } elseif (\spl_object_id($item) !== \spl_object_id($this->itemInstances[$item->getKey()])) { throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); } @@ -457,7 +457,7 @@ public function save(CacheItemInterface $item): bool if ($this->getConfig()->isUseStaticItemCaching()) { $this->itemInstances[$item->getKey()] = $item; } - } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { + } elseif (\spl_object_id($item) !== \spl_object_id($this->itemInstances[$item->getKey()])) { throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); } @@ -542,7 +542,7 @@ protected function deregisterItems(array $itemKeys): static */ public function attachItem(CacheItemInterface $item): static { - if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { + if (isset($this->itemInstances[$item->getKey()]) && \spl_object_id($item) !== \spl_object_id($this->itemInstances[$item->getKey()])) { throw new PhpfastcacheLogicException( 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.' @@ -563,7 +563,7 @@ public function attachItem(CacheItemInterface $item): static public function isAttached(CacheItemInterface $item): bool { if (isset($this->itemInstances[$item->getKey()])) { - return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); + return \spl_object_id($item) === \spl_object_id($this->itemInstances[$item->getKey()]); } return false; } diff --git a/lib/Phpfastcache/EventManager.php b/lib/Phpfastcache/EventManager.php index 9c646575..83c26034 100644 --- a/lib/Phpfastcache/EventManager.php +++ b/lib/Phpfastcache/EventManager.php @@ -186,7 +186,7 @@ public function setItemPoolContext(ExtendedCacheItemPoolInterface $pool): EventM $this->onEveryEvents(function (string $eventName, ...$args) { EventManager::getInstance()->dispatch($eventName, ...$args); - }, 'Scoped' . $pool->getDriverName() . spl_object_hash($this)); + }, 'Scoped' . $pool->getDriverName() . spl_object_id($this)); return $this; } diff --git a/tests/cases/CacheClearSingleInstance.test.php b/tests/cases/CacheClearSingleInstance.test.php index d2ad5d1c..dc4a985f 100644 --- a/tests/cases/CacheClearSingleInstance.test.php +++ b/tests/cases/CacheClearSingleInstance.test.php @@ -40,9 +40,9 @@ $testHelper->assertFail('A single cache instance have NOT been cleared'); } -$driverInstance2Hash = spl_object_hash($driverInstance2); +$driverInstance2Hash = spl_object_id($driverInstance2); foreach ($cacheInstances as $cacheInstance) { - $driverInstanceHash = spl_object_hash($cacheInstance); + $driverInstanceHash = spl_object_id($cacheInstance); if ($driverInstanceHash !== $driverInstance2Hash) { $testHelper->assertPass("Compared cache instance #{$driverInstanceHash} does not match with previously cleared cache instance #" . $driverInstance2Hash); } else {