PHP Static vs Instance Methods and Properties

Static and instance methods look similar but behave very differently — mixing them up is a common source of confusion for developers coming from more strictly-typed languages.

class Counter {
    private static int $totalCount = 0;
    private int $id;

    public function __construct() {
        self::$totalCount++;
        $this->id = self::$totalCount;
    }

    // Instance method: needs an object, accesses $this
    public function getId(): int {
        return $this->id;
    }

    // Static method: called on the class itself, no $this
    public static function getTotalCount(): int {
        return self::$totalCount;
    }
}

$a = new Counter();
$b = new Counter();
echo Counter::getTotalCount(); // 2, no instance needed
echo $a->getId();              // 1

Use static methods and properties for data or behavior that belongs to the class as a whole (a counter, a factory method), and instance methods for anything tied to a specific object’s own state.