HH\Vector::pop
Remove the last element of the current Vector
and return it
public function pop(): Tv;
This function throws an exception if the current Vector
is empty.
The current Vector
will have n - 1
elements after this operation, where
n
is the number of elements in the current Vector
prior to the call to
pop()
.
Returns
Tv
- The value of the last element.
Examples
This example shows that pop()
returns the last element and removes it from the Vector
:
$v = Vector {'red', 'green', 'blue', 'yellow'};
$last_color = $v->pop();
var_dump($last_color);
var_dump($v);
string(6) "yellow"
object(HH\Vector)#1 (3) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
[2]=>
string(4) "blue"
}
This example shows that trying to pop
from an empty Vector
will throw an exception:
$v = Vector {};
$last_element = $v->pop(); // Throws InvalidOperationException
Fatal error: Uncaught exception 'InvalidOperationException' with message 'Cannot pop empty Vector' in /user-documentation/api-examples/class.Vector/pop/002-throw-exception.php:7
Stack trace:
#0 /user-documentation/api-examples/class.Vector/pop/002-throw-exception.php(7): HH\Vector->pop()
#1 {main}