3v4l.org

run code in 300+ PHP versions simultaneously
<?php class WeakMap2 { private array $weakRefs = []; private array $values = []; public function offsetSet($object, $value) : void { $id = spl_object_id($object); $this->weakRefs[$id] = \WeakReference::create($object); $this->values[$id] = $value; } } class WeakAnalysingMapRepro { public array $valueWithOwnerCountByIndex = []; private WeakMap2 $ownerDestructorHandlers; public function __construct() { $this->ownerDestructorHandlers = new WeakMap2(); $this->addKeyOwner(new \DateTime()); } protected function addKeyOwner(object $owner) { $handler = new class($this) { private \WeakReference $weakAnalysingMap; public function __construct(WeakAnalysingMapRepro $analysingMap) { $this->weakAnalysingMap = \WeakReference::create($analysingMap); } public function __destruct() { $analysingMap = $this->weakAnalysingMap->get(); var_dump(array_keys($analysingMap->valueWithOwnerCountByIndex)); \Closure::bind(static function () use ($analysingMap) { var_dump(array_keys($analysingMap->valueWithOwnerCountByIndex)); }, null, WeakAnalysingMapRepro::class)(); } public function addReference($index): void { $analysingMap = $this->weakAnalysingMap->get(); $analysingMap->valueWithOwnerCountByIndex[$index] = true; } }; $this->ownerDestructorHandlers->offsetSet($owner, $handler); $handler->addReference(10); } } $map = new WeakAnalysingMapRepro(); unset($map); echo 'DONE';
Output for 8.2.18 - 8.2.29, 8.3.5 - 8.3.25, 8.4.1 - 8.4.12
Warning: Attempt to read property "valueWithOwnerCountByIndex" on null in /in/h7ZER on line 44 Fatal error: Uncaught TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in /in/h7ZER:44 Stack trace: #0 /in/h7ZER(44): array_keys(NULL) #1 /in/h7ZER(64): class@anonymous->__destruct() #2 {main} thrown in /in/h7ZER on line 44
Process exited with code 255.
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
array(1) { [0]=> int(10) } array(1) { [0]=> string(12) "analysingMap" } DONE

preferences:
70.28 ms | 407 KiB | 5 Q