- var_dump: documentation ( source)
- spl_object_id: documentation ( source)
- array_keys: documentation ( source)
<?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';