Instance vs. Class

So far, every property and method we've created belongs to an instance of a class (an object). You have to create an object first ($user = new User()) before you can call its methods ($user->getName()). The value of $user->name is unique to that specific object.

But what if you need a property or method that belongs to the class itself, not to any individual object? This is what the static keyword is for. Static members are shared across all instances of a class.

Think of it this way: a blueprint for a house (the class) can have a general note on it, like "All houses built from this plan should use Brand X windows." This note is part of the blueprint itself (static property). Each individual house built (the object) will have its own unique address and color (instance properties).


1. Static Properties

A static property is a variable that is shared among all objects of a class. There is only one copy of the static property, no matter how many objects you create.

How to use:

  • Declare it with the static keyword.
  • Access it from inside the class using self::$propertyName.
  • Access it from outside the class using ClassName::$propertyName.

Example: A Counter
Let's create a User class that counts how many user objects have been created.


class User {
    // This property belongs to the class, not to any one user.
    public static $userCount = 0;
    
    public $name;

    public function __construct($name) {
        $this->name = $name;
        
        // When a new user is created, increment the static counter.
        self::$userCount++;
    }
}

echo "Initial user count: " . User::$userCount; // Outputs: 0

$user1 = new User("Alice");
$user2 = new User("Bob");

echo "<br>";
echo "User count after creating objects: " . User::$userCount; // Outputs: 2

2. Static Methods

A static method can be called directly on the class, without creating an instance of the class first. They are often used for "utility" or "helper" functions that are related to the class but don't need an actual object to work.

The Golden Rule: Because they are not tied to an object, static methods cannot use the $this variable.

Example: A Validator Helper
Let's add a static method to a Validator class to check if an email is valid.


class Validator {
    // This method can be called without creating a Validator object.
    public static function isEmailValid($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
    }
}

// No need to write $validator = new Validator();
if (Validator::isEmailValid("test@example.com")) {
    echo "The email is valid.";
} else {
    echo "The email is invalid.";
}

When to Use Static?

  • Use static properties for data that should be the same for every object of that class (e.g., configuration values, counters).
  • Use static methods for helper or utility functions that perform a task related to the class but don't depend on any single object's state.
  • Avoid overusing static. The core of OOP is about objects. If a method needs to know about an object's specific data (like a user's name), it should almost always be a regular (non-static) method.