HH\Map::toMap

Returns a deep copy of the current Map

public function toMap(): Map<Tk, Tv>;

Returns

  • Map<Tk, Tv> - a Map that is a deep copy of the current Map.

Examples

This example shows that toMap returns a deep copy of the Map $m. Mutating the new Map $m2 doesn't affect the original Map.

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
};

$m2 = $m->toMap();
$m2->add(Pair {'purple', '#663399'});

var_dump($m);
var_dump($m2);
Output
object(HH\Map)#1 (4) {
  ["red"]=>
  string(7) "#ff0000"
  ["green"]=>
  string(7) "#00ff00"
  ["blue"]=>
  string(7) "#0000ff"
  ["yellow"]=>
  string(7) "#ffff00"
}
object(HH\Map)#2 (5) {
  ["red"]=>
  string(7) "#ff0000"
  ["green"]=>
  string(7) "#00ff00"
  ["blue"]=>
  string(7) "#0000ff"
  ["yellow"]=>
  string(7) "#ffff00"
  ["purple"]=>
  string(7) "#663399"
}