Static properties in PHP belong to the class itself, rather than any object of the class. They are shared across all instances of the class and can be accessed without creating an instance.
Key Features of Static Properties
Declared with the static Keyword:
Static properties must be declared using the static keyword.
Accessed via Class Name:
Access static properties using the class name, double colon ::, and the property name.
Shared Across Instances:
All instances of the class share the same static property.
Cannot Use $this:
Static properties are not tied to any specific instance, so $this cannot be used to access them.
<?php class ClassName { public static $staticProp = "Static Value"; } // Access the static property echo ClassName::$staticProp; ?>
Example 1: Basic Static Property
<?php class Pi { public static $value = 3.14159; } // Access the static property directly echo Pi::$value; // Outputs: 3.14159 ?>
Example 2: Static and Non-Static Methods
Static properties can be accessed from within a non-static method using the self keyword.
<?php class Pi { public static $value = 3.14159; public function getStaticValue() { // Access static property using self:: return self::$value; } } $pi = new Pi(); echo $pi->getStaticValue(); // Outputs: 3.14159 ?>
Example 3: Inheritance and Static Properties
Static properties can be accessed in child classes using the parent keyword.
<?php class Pi { public static $value = 3.14159; } class X extends Pi { public function getParentStaticValue() { // Access parent's static property return parent::$value; } } // Access static property directly via the child class echo X::$value; // Outputs: 3.14159 // Access static property via a method in the child class $x = new X(); echo $x->getParentStaticValue(); // Outputs: 3.14159 ?>
Example 4: Modifying Static Properties
Static properties can be updated, and the change will reflect across all instances.
<?php class Counter { public static $count = 0; public function increment() { self::$count++; } } // Increment the static property $counter1 = new Counter(); $counter1->increment(); $counter2 = new Counter(); $counter2->increment(); echo Counter::$count; // Outputs: 2 (shared across instances) ?>
Key Points to Remember
Shared Across All Instances:
Static properties are shared by all instances of the class.
Access Levels:
Static properties can have public, protected, or private visibility.
No Instance Required:
You can access static properties directly using the class name.
Static and Non-Static Coexist:
A class can have both static and non-static properties.
Cannot Use $this:
Use self:: or parent:: to access static properties, as $this is not available in static contexts.
When to Use Static Properties
Counters or Shared Data:
To keep track of values that should be consistent across all instances, such as counters or configuration values.
Constants:
Use static properties for values that need to remain the same across all objects but can still be updated if required.
Caching:
Store reusable data that multiple instances might need to access, like database connections or pre-calculated values.
Conclusion
Static properties in PHP are a powerful feature for defining class-level data that can be shared across all instances. They promote better memory management and provide a centralized way to manage shared data. Understanding static properties and their proper use can significantly enhance the organization and performance of your code.