Every property and method covered so far belongs to a specific object — $user1 and $user2 each have their own separate copy. A static member belongs to the class itself, shared identically across every object of that class.
class User {
public static int $totalUsers = 0;
public function __construct() {
self::$totalUsers++;
}
}
new User();
new User();
new User();
echo User::$totalUsers; // 3Note the syntax difference: a static property is accessed with :: and no $ before the name (User::$totalUsers), never with -> on an object.
Inside the class, self:: refers to the static member — using the class name directly works too, but self:: stays correct even if the class is later renamed.
Called on the class itself, without needing an object instance at all.
class MathHelper {
public static function square(int $n): int {
return $n * $n;
}
}
echo MathHelper::square(5); // 25 — no "new MathHelper()" needed| Use static when... | Use a regular (instance) method when... |
|---|---|
| The logic doesn't depend on any particular object's data | The logic reads or changes a specific object's properties |
| A utility function grouped inside a class for organization | The method genuinely represents something a single object does |
| A counter or shared value across every instance | Each object needs its own independent value |
The main limitation
A static method can't access $this — it has no specific object to refer to. Reaching for static as a default, rather than when it genuinely fits, is a common beginner overuse.