HH\Vector::toVector

Description

public function toVector(): Vector<Tv>

Returns a copy of the current Vector.

Return Values

Examples

This example shows how toVector() returns a deep copy of $v, so mutating this new Vector doesn't affect the original.

<?hh

namespace Hack\UserDocumentation\API\Examples\Vector\ToVector;

$v = Vector {'red', 'green', 'blue', 'yellow'};

// Make a deep Vector copy of $v
$v2 = $v->toVector();

// Modify $v2 by adding an element
$v2->add('purple');
var_dump($v2);

// The original Vector $v doesn't include 'purple'
var_dump($v);
Output
object(HH\Vector)#2 (5) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(4) "blue"
  [3]=>
  string(6) "yellow"
  [4]=>
  string(6) "purple"
}
object(HH\Vector)#1 (4) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(4) "blue"
  [3]=>
  string(6) "yellow"
}