3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Weakmaps accepts objects and arrays as keys $source = new Weakmap(); $key = (object) ['a' => 2]; $source[$key] = 1; // Yield may emit objects and arrays as keys $source = function () { yield (object) ['a' => 3] => 1; }; // This is just for illustration $source = $source(); // An iterator may return objects and arrays as keys(mixed, in fact) class myIterator implements Iterator { private int $position = 0; public function rewind(): void { $this->position = 0; } public function current(): mixed { return 1; } public function key(): mixed { return (object) ['a' => 4]; } public function next(): void { ++$this->position; } public function valid(): bool { return $this->position == 0; } } $source = new myIterator(); foreach($source as $key => $value) { print get_class($key); // Stdclass print $value; // 1 }
Output for 8.2.31 - 8.2.32, 8.3.0 - 8.3.33, 8.4.1 - 8.4.24, 8.5.0 - 8.5.9
stdClass1

preferences:
42.31 ms | 774 KiB | 4 Q