php-ds / ext-ds

An extension providing efficient data structures for PHP 7
https://medium.com/p/9dda7af674cd
MIT License
2.11k stars 95 forks source link

Map::toArray() and classes #131

Closed Frago9876543210 closed 5 years ago

Frago9876543210 commented 5 years ago
<?php
class A{}

$map = new \Ds\Map();

$map->put(new A, 0);
$map->put(new A, 1);

var_dump($map->toArray());
Warning: Illegal offset type in bug.php on line 9
Warning: Illegal offset type in bug.php on line 9
array(0) {
}

I think it should return array with spl_object_hash($class) => value

rtheunissen commented 5 years ago

This is expected behaviour.

toArray will attempt to create an associative array with the same keys as the map. It is not possible to use the hash value, because the hash and the key are not equal. For example, if three objects have the same hash but are not equal, all three should be in the map.

If you use objects as keys, toArray will fail.