PHP OOP Static Properties

PHP Tutorial

 PHP OOP – Static Properties

In PHP OOP Static Properties belongs to the class, not to individual objects.
  •  Shared by all objects
  •  Accessed using ClassName::$property 
  •  Declared with static keyword

 Basic Static Property Example


 

You don’t need to create an object.


 Access Static Property Inside Class

Use self::$property inside the same class.


 


 Modify Static Property


 


All Objects Share Same Static Property


 

All objects update one shared variable.


 Static Property with Inheritance


 

Child class inherits static properties.


  Override Static Property in Child


 

Each class keeps its own version.


  Late Static Binding with Static Properties

Use static::$property for overriding behavior.


 


 Wrong: Cannot Use $this with Static Property


 

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


  Practical Use Case – Configuration


 


  Practical Use Case – Auto Incrementing ID


 


 Summary (Easy to Remember)

FeatureMeaning
static propertyBelongs to class, not object
Access usingClassName::$property
All objects share Yes
Cannot use $thisYes
Good forCounters, settings, shared data

You may also like...