HH\Pair::at

Returns the value at the specified key in the current Pair.

Description

public function at(
  int $k,
): mixed

Returns the value at the specified key in the current Pair.

If the key is not present, an exception is thrown. This essentially means if you specify a key other than 0 or 1, you will get an exception. If you don't want an exception to be thrown, use get() instead.

$v = $p->at($k)" is semantically equivalent to $v = $p[$k].

Parameters

  • int $k - the key from which to retrieve the value.

Return Values

  • mixed - The value at the specified key; or an exception if the key does not exist.

Examples

This example prints the first and second values of the Pair:

<?hh

namespace Hack\UserDocumentation\API\Examples\Pair\At\ExistingKey;

$p = Pair {'foo', -1.5};

// Print the first element
var_dump($p->at(0));

// Print the second element
var_dump($p->at(1));
Output
string(3) "foo"
float(-1.5)

This example throws an OutOfBoundsException because a Pair only has the indexes 0 and 1:

<?hh

namespace Hack\UserDocumentation\API\Examples\Pair\At\MissingKey;

$p = Pair {'foo', -1.5};

// Index 2 doesn't exist because pairs always have exactly two elements
var_dump($p->at(2));
Output
Fatal error: Uncaught exception 'OutOfBoundsException' with message 'Integer key 2 is out of bounds' in /user-documentation/api-examples/class.Pair/at/002-missing-key.php:8
Stack trace:
#0 /user-documentation/api-examples/class.Pair/at/002-missing-key.php(8): HH\Pair->at()
#1 {main}