Static methods and properties belong to the class itself, not to any specific object. Access them with ClassName::method() without creating an instance. Useful for utility functions, counters, and singleton patterns.
Static Methods and Properties
class Counter {
private static int $count = 0;
public static function increment(): void {
self::$count++;
}
public static function getCount(): int {
return self::$count;
}
// Factory method pattern
public static function create(string $name): self {
$obj = new self();
// setup
return $obj;
}
}
Counter::increment();
Counter::increment();
echo Counter::getCount(); // 2
// Constants
class Status {
const ACTIVE = "active";
const INACTIVE = "inactive";
const BANNED = "banned";
}
echo Status::ACTIVE; // active