# HH\Vector::toVector

Returns a copy of the current `Vector`

``````public function toVector(): Vector<Tv>;
``````

## Returns

• `Vector<Tv>` - A `Vector` that is a copy of the current `Vector`.

## Examples

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

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

// Make a new Vector that is a copy of \$v (i.e. contains the same elements)
\$v2 = \$v->toVector();

// Modify \$v2 by adding an element
var_dump(\$v2);

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

This example shows how `toVector()` returns a shallow copy of `\$v` (a new `Vector` object containing the same elements) rather than a deep copy (a new `Vector` object containing copies of the elements of `\$v` that are themselves objects).

Thus, mutating an element of `\$v` that is itself an object also mutates the corresponding element of `\$v2`, since the element in `\$v` is the same object as the element in `\$v2`.

``````\$inner = Vector {1, 2, 3};
\$v = Vector {'a', \$inner, 'c'};

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

// Modify the original Vector \$v's inner Vector by adding an element

// The original Vector \$v's inner Vector includes 4.
var_dump(\$v);

// The new Vector \$v2's inner Vector also includes 4. toVector() only does a shallow copy.
var_dump(\$v2);
``````
Output
``````object(HH\Vector)#2 (3) {
=>
string(1) "a"
=>
object(HH\Vector)#1 (4) {
=>
int(1)
=>
int(2)
=>
int(3)
=>
int(4)
}
=>
string(1) "c"
}
object(HH\Vector)#3 (3) {
=>
string(1) "a"
=>
object(HH\Vector)#1 (4) {
=>
int(1)
=>
int(2)
=>
int(3)
=>
int(4)
}
=>
string(1) "c"
}
``````