HH\Shapes::keyExists

Check if a field in shape exists

public static function keyExists<T as shape()>(
  darray $shape,
  arraykey $index,
): bool;

Similar to array_key_exists, but for shapes.

Parameters

  • darray $shape
  • arraykey $index

Returns

  • bool

Examples

This example shows that keyExists returns true if a key exists in the Shape (even if the corresponding value is NULL):

function run(shape(?'x' => ?int, ?'y' => ?int, ?'z' => ?int) $point): void {
  // The key 'x' exists in Shape $point
  \var_dump(Shapes::keyExists($point, 'x'));

  // The key 'z' doesn't exist in $point
  \var_dump(Shapes::keyExists($point, 'z'));

  // The key 'y' exists in $point, even though its value is NULL
  \var_dump(Shapes::keyExists($point, 'y'));
}

run(shape('x' => 3, 'y' => null));
Output
bool(true)
bool(false)
bool(true)