HH\Map::__construct

Description

public function __construct(
  ?KeyedTraversable<Tk,Tv> $it,
): void

Creates a Map from the given KeyedTraversable, or an empty Map if null is passed.

Parameters

  • ?KeyedTraversable<Tk,Tv> $it - any Traversable object from which to create a Map (e.g., array). If null, then an empty Map is created.

Examples

This example shows how to create a Map from various KeyedTraversables:

<?hh

namespace Hack\UserDocumentation\API\Examples\Map\__construct;

// Create a new string-keyed Map from an associative array
$m = new Map(array(
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
));
var_dump($m);

// Create a new integer-keyed Map from a Vector
$m = new Map(Vector {'red', 'green', 'blue', 'yellow'});
var_dump($m);
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 (4) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(4) "blue"
  [3]=>
  string(6) "yellow"
}

This example shows how passing null to the constructor creates an empty Map:

<?hh

namespace Hack\UserDocumentation\API\Examples\Map\__construct\FromNull;

// An empty Map is created if null is provided
$m = new Map(null);
var_dump($m);
Output
object(HH\Map)#1 (0) {
}