In Object-Oriented Programming, most properties and methods belong to an instance (the object) of a class. Static members are different: they belong to the class itself and can be used without creating an object. They are crucial for utility functions, configuration, and factory methods.
1. The static Keyword
You define a static property or method by simply using the static keyword.
2. Static Properties (Shared Data)
A static property holds data that is shared across all instances of a class. There is only one copy of this data, regardless of how many objects are created.
- Use Case: Tracking the total number of items created, holding global application configuration settings.
Accessing Static Properties
- Inside the Class: Use
self::followed by the property name (prefixed with a dollar sign$). - Outside the Class: Use the Class Name followed by the double colon operator (
::) and the property name (prefixed with a dollar sign$).
Example: Tracking Total Instances
<?php
class Counter {
// Static property to track total objects created (Shared by all)
public static $totalItems = 0;
public function __construct() {
// Access static property using self::
self::$totalItems++;
}
}
$a = new Counter();
$b = new Counter();
$c = new Counter();
// Access outside the class using the class name and ::
echo "Total items created: " . Counter::$totalItems . "<br>";
// Output: Total items created: 3
// Changing the static property affects all future checks:
Counter::$totalItems = 100;
echo "New total: " . Counter::$totalItems;
// Output: New total: 100
?>
3. Static Methods (Utility Tools)
A static method is a function that performs a task related to the class but does not rely on any specific object data ($this).
- Use Case: Mathematical helpers, general data cleaning, or formatting utilities where you don’t need to instantiate an object just to call one function.
Accessing Static Methods
- Inside the Class: Use
self::methodName(). - Outside the Class: Use the Class Name followed by the double colon operator (
::) and the method name.
Example: A Simple Math Utility
<?php
class MathHelper {
// This is a utility function that doesn't need object data
public static function calculatePercentage($part, $whole) {
if ($whole == 0) {
return 0;
}
return round(($part / $whole) * 100, 2);
}
// Static methods CANNOT access non-static properties (no $this)
public static function getPi() {
return pi();
}
}
// Call the static method directly on the class (no 'new MathHelper()')
$score = MathHelper::calculatePercentage(45, 60);
echo "Score: " . $score . "%<br>";
// Output: Score: 75%
// Calling another static method:
echo "Pi value: " . MathHelper::getPi();
// Output: Pi value: 3.1415926535898
?>
4. The Scope Resolution Operator (::)
The double colon operator (::) is officially called the Scope Resolution Operator or colloquially, the Paamayim Nekudotayim (Hebrew for “double colon”).
It is the standard way in PHP to refer to static properties, static methods, and constants defined within a class.
⏭️ Next Steps
You have now mastered the concept of static members. The remaining pending OOP topics are: Destructor, Traits, Class Constants, and Iterables.
