💾 PHP Static Properties – Share Data Across All Class Instances

Learn how to define and use static properties in PHP to store class-level data shared by all instances or accessed without instantiation.


🧲 Introduction – What Are Static Properties in PHP?

Static properties in PHP belong to a class itself, not to any object (instance). This means they are:

  • Shared across all instances of that class
  • Accessible without creating an object
  • Useful for shared counters, config values, or constants

🎯 In this guide, you’ll learn:

  • How to define and access static properties
  • Use cases for global counters and shared values
  • Best practices to avoid static overuse

🧱 1. Defining and Accessing Static Properties

class Counter {
    public static $count = 0;
}

echo Counter::$count; // 0

➡️ Use the static keyword to declare the property.
➡️ Access it using ClassName::$property.


🔄 2. Modifying Static Properties

class Counter {
    public static $count = 0;

    public static function increment() {
        self::$count++;
    }
}

Counter::increment();
echo Counter::$count; // 1

➡️ Use self:: inside the class to access static members.
➡️ Any change affects all uses of that class globally.


🔁 3. Static Property Shared Across Instances

class VisitTracker {
    public static $visits = 0;

    public function __construct() {
        self::$visits++;
    }
}

$a = new VisitTracker();
$b = new VisitTracker();
echo VisitTracker::$visits; // 2

➡️ Every object increments the same $visits property.
➡️ All instances share the same static data.


🏗️ 4. Static Properties in Inheritance

class Base {
    protected static $message = "Hello";
    
    public static function show() {
        echo static::$message;
    }
}

class Child extends Base {
    protected static $message = "Hi!";
}

Child::show(); // Hi!

➡️ Static properties support late static binding with static::.
➡️ Useful for base class utilities overridden in child classes.


🚫 5. Static vs Non-Static Properties

FeatureStatic PropertyNon-Static Property
ScopeClass-levelObject-level
Shared?Yes, shared by allNo, unique per object
Requires object❌ No✅ Yes
Use caseCounters, config, logsUser data, instance state

🧠 Best Practices

  • ✅ Use for shared values across all class instances
  • ✅ Combine with static methods for utility classes
  • ❌ Avoid using static properties to store user state
  • ✅ Use self::$var (in class) or Class::$var (outside class)
  • ❌ Don’t overuse — too many statics reduce modularity

📌 Summary – Recap & Next Steps

Static properties in PHP are shared variables at the class level. They help you store shared state or configuration without needing object creation.

🔍 Key Takeaways:

  • Declared with static, accessed with Class::$prop
  • Shared across all instances of the class
  • Use for global counters, flags, config, or singleton patterns

⚙️ Real-World Use Cases:
User counters, logger status, shared configurations, cache pools, class-wide limits


❓ Frequently Asked Questions (FAQs)

❓ Can I access a static property from an object?
✅ Yes, but it’s better to use ClassName::$prop instead of $object::$prop.

❓ Can static properties be private or protected?
✅ Yes. Use access modifiers like private static or protected static.

❓ Do static properties reset between object instances?
❌ No. They are shared — changes persist across instances.

❓ What is the difference between self::$var and static::$var?
self is fixed to the class it’s written in; static supports late static binding.

❓ Can I store user session data in static properties?
❌ Not recommended. Use session handling ($_SESSION) or object properties instead.


Share Now :
Share

💾 PHP Static Properties

Or Copy Link

CONTENTS
Scroll to Top