Skip to main content

__construct

Note

This is a point-in-time snapshot of the API documentation from January 2026. Going forward, we will not be maintaining a public copy of these references, and recommend users to refer to the built-in signature helpers available in the Hack LSP instead for complete and up-to-date information.

Creates a Set from the given Traversable, or an empty Set if null is passed

public function __construct(
?Traversable<Tv> $iterable = NULL,
): void;

Parameters

Returns

  • void

Examples

This example shows how to create a Set from various Traversables. Notice that duplicate values in the input Traversables only appear once in the output Set.

// Create a new Set from an array
$s = new Set(varray['red', 'green', 'red', 'blue', 'blue', 'yellow']);
\var_dump($s);

// Create a new Set from a Vector
$s = new Set(Vector {'red', 'green', 'red', 'blue', 'blue', 'yellow'});
\var_dump($s);

// Create a new Set from the values of a Map
$s = new Set(Map {
'red1' => 'red',
'green' => 'green',
'red2' => 'red',
'blue1' => 'blue',
'blue2' => 'blue',
'yellow' => 'yellow',
});
\var_dump($s);

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

// An empty Set is created if null is provided
$s = new Set(null);
\var_dump($s);