HH\invariant

Ensure that an invariant is satisfied

namespace HH;

function invariant(
  mixed $condition,
  FormatString<\PlainSprintf> $format_str,
  ...$f_args,
): void;

If it fails, it calls invariant_violation

This function provides a way to have a variable type checked as a more specific type than it is currently declared. A source transformation in the runtime modifies code that looks like:

invariant(<condition>, 'sprintf format: %s %d', 'string', ...);

... is transformed to be:

  if (!(<condition>)) { // an Exception is thrown
    invariant_violation('sprintf format: %s', 'string', ...);
  }
  // <condition> is known to be true in the code below

See also:

Parameters

  • mixed $condition
  • FormatString<\PlainSprintf> $format_str - The string that will be displayed when your invariant fails, with possible placeholders.
  • ...$f_args

Returns

  • void