3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Name implements \Stringable { public function __construct(public readonly string $name) { } public function __toString(): string { return $this->name; } } enum Value: string { case id = 'id'; } function create(\WeakMap $map): \WeakMap { $name = new Name('func'); if (!isset($map[$name])) { $map[$name] = 'func-val'; } echo "In function scope:\n\n"; var_dump($map); echo PHP_EOL; return $map; } $name = new Name('global'); $map = new \WeakMap(); $map[$name] = 'global-val'; $map = create($map); echo "\n"; echo "In global scope:\n\n"; // watch the `Name('func')` key not being present here var_dump($map);
Output for 8.1.30, 8.2.3 - 8.2.26, 8.3.5 - 8.3.14
In function scope: object(WeakMap)#2 (2) { [0]=> array(2) { ["key"]=> object(Name)#1 (1) { ["name"]=> string(6) "global" } ["value"]=> string(10) "global-val" } [1]=> array(2) { ["key"]=> object(Name)#3 (1) { ["name"]=> string(4) "func" } ["value"]=> string(8) "func-val" } } In global scope: object(WeakMap)#2 (1) { [0]=> array(2) { ["key"]=> object(Name)#1 (1) { ["name"]=> string(6) "global" } ["value"]=> string(10) "global-val" } }

preferences:
30.36 ms | 407 KiB | 5 Q