Source Code Fundamentals: Names

Names are used to label variables, constants, functions, and user-defined types, among other things. A name must begin with an upper- or lowercase letter or underscore, which can optionally be followed by those same characters or decimal digits. Furthermore, variable names (which includes function parameter names) and property names must be preceded by $. For example:

<?hh // strict

namespace Hack\UserDocumentation\Fundamentals\Names\Examples\SomeNames;

class Data {
  const int MAX_VALUE = 100;
  private int $value = 0;
  /* ... */
}
interface ICollection { /* ... */ }
enum Position: int {
  Top = 0;
  Bottom = 1;
  Left = 2;
  Right = 3;
  Center = 4;
}
function compute(int $val): void {
  $count = $val + 1;
  /* ... */
}

The name $_, referred to as the placeholder variable, is reserved for use in the list intrinsic function and the foreach statement.

The name $this is predefined inside any instance method or constructor when that method is called from within an object context. $this is read-only and designates the object on which the method is being called, or the object being constructed. The type of $this is this.

Names beginning with two underscores (__) are reserved by the Hack language.