📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners OOP — Static Methods and Properties

OOP — Static Methods and Properties

5 min read Quiz at the end
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
Topic Quiz · 5 questions

Test your understanding before moving on

1. How do you call a static method?
💡 Static methods are called with ClassName::method() or self::method() inside the class.
2. Can static methods access $this?
💡 Static methods have no $this — they belong to the class, not an instance.
3. What is self:: vs static:: (late static binding)?
💡 static:: uses late static binding — resolves to the actual called class at runtime.
4. How do you define a class constant?
💡 class constants use const NAME = value; — no dollar sign.
5. Can static properties be inherited?
💡 Static properties are inherited, but each class maintains its own value with late static binding.