PHP OOP Static Properties

🧲 PHP OOP – Static Properties

A static property belongs to the class, not to individual objects.

✔ Shared by all objects
✔ Accessed using ClassName::$property
✔ Declared with static keyword


🟦 1️⃣ Basic Static Property Example


 

You don’t need to create an object.


🟩 2️⃣ Access Static Property Inside Class

Use self::$property inside the same class.


 


🟧 3️⃣ Modify Static Property


 


🟥 4️⃣ All Objects Share Same Static Property


 

All objects update one shared variable.


🟨 5️⃣ Static Property with Inheritance


 

Child class inherits static properties.


🟫 6️⃣ Override Static Property in Child


 

Each class keeps its own version.


🟪 7️⃣ Late Static Binding with Static Properties

Use static::$property for overriding behavior.


 


🟦 8️⃣ Wrong: Cannot Use $this with Static Property


 

Because $this refers to an object, not the class.


🟩 9️⃣ Practical Use Case – Configuration


 


🟧 🔟 Practical Use Case – Auto Incrementing ID


 


🎯 Summary (Easy to Remember)

Feature Meaning
static property Belongs to class, not object
Access using ClassName::$property
All objects share ✔ Yes
Cannot use $this
Good for Counters, settings, shared data

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *