Skip to main content

Class

Hack supports passing references to classes for use in instatiation and static member access.

<<__ConsistentConstruct>>
class Employee {
public static function getKind(): int { return 4; };
}

function f(class<Employee> $cls): void {
$w = new $cls(); // create an object of $cls

$i = $cls::getKind();
}

Function f can be called with a reference to the class Employee or any of its subclasses using ::class literals (SomeClassName::class).

class Intern extends Employee {
// ...
}

function demo(): void {
f(Employee::class); // will call: new Employee();
f(Intern::class); // will call: new Intern();
f(Vector::class); // typechecker error!
}