-
Hajipur, Bihar, 844101
In PHP, static properties are class-level variables that are shared among all instances of a class. Unlike regular object properties, static properties belong to the class itself rather than any individual object. This allows developers to store shared data, counters, configuration values, or constants that are accessible without creating an object.
Static properties are often used together with static methods to create utility classes, singleton patterns, or counters in PHP applications.
A static property is declared with the static keyword inside a class. It can be accessed directly using the scope resolution operator ::. Key characteristics include:
Belongs to the class, not an instance.
Shared across all instances of the class.
Can be accessed without creating an object.
Can be accessed and modified inside static methods using self::$propertyName.
class Counter {
public static $count = 0;
public static function increment() {
self::$count++;
}
public static function getCount() {
return self::$count;
}
}
// Access without creating an object
Counter::increment();
Counter::increment();
echo Counter::getCount(); // Outputs: 2
Here, $count is a static property shared across all uses of the class. Each time increment() is called, the same $count is updated.
| Feature | Static Property | Instance Property |
|---|---|---|
| Belongs to | Class | Object |
| Shared among objects | Yes | No |
| Access | ClassName::$property | $object->property |
| Use cases | Counters, configuration, shared state | Object-specific data |
Static properties are ideal for data that should be shared across all objects, while instance properties store data specific to individual objects.
Static properties are accessed using the scope resolution operator ::, either inside or outside the class:
class Settings {
public static $appName = "MyApp";
}
// Access without creating an object
echo Settings::$appName; // Outputs: MyApp
Inside the class, static properties are accessed using self::$propertyName:
class User {
public static $userCount = 0;
public function __construct() {
self::$userCount++;
}
public static function getUserCount() {
return self::$userCount;
}
}
$user1 = new User();
$user2 = new User();
echo User::getUserCount(); // Outputs: 2
Static properties can be modified either inside static methods or from outside the class using ClassName::$property:
class BankAccount {
public static $totalAccounts = 0;
public static function newAccount() {
self::$totalAccounts++;
}
}
BankAccount::newAccount();
BankAccount::newAccount();
echo BankAccount::$totalAccounts; // Outputs: 2
This shared property ensures all instances reflect the same value, useful for counters or global state tracking.
Static properties can be inherited by child classes. However, if a child class overrides a static property, it becomes separate from the parent:
class ParentClass {
public static $value = 10;
}
class ChildClass extends ParentClass {
public static $value = 20;
}
echo ParentClass::$value; // Outputs: 10
echo ChildClass::$value; // Outputs: 20
This demonstrates that static properties can maintain class-specific values while still being shared within the same class hierarchy.
Counters: Track the number of objects created.
Configuration storage: Store global configuration values accessible to all instances.
Singleton pattern: Hold the single instance of a class.
Shared resources: Track global states like active users, database connections, or API limits.
Example – counting objects:
class Employee {
public static $count = 0;
public function __construct() {
self::$count++;
}
}
new Employee();
new Employee();
echo Employee::$count; // Outputs: 2
Use static properties for shared class-level data, not for instance-specific values.
Access static properties using self::$property inside the class and ClassName::$property outside.
Avoid overusing static properties to store mutable data; this can make code hard to test.
Combine static properties with static methods for utility or singleton classes.
Keep naming clear to indicate that the property is shared across the class.
Trying to access static properties via $this->property.
Overusing static properties to store instance-specific data.
Modifying static properties from multiple unrelated classes, causing unexpected side effects.
Forgetting the self:: or ClassName:: prefix when referencing static properties.
Static properties belong to the class itself and are shared among all instances.
They are accessed using self::$property or ClassName::$property.
Static properties are ideal for counters, configuration, shared data, or singleton patterns.
Proper use ensures reusability, maintainability, and efficient resource management in PHP applications.
Understanding static properties is essential for PHP developers to write scalable and organized code, especially when managing shared data or global state efficiently.
Create a class Counter with a static property $count initialized to 0 and a method increment() that increases $count by 1. Test by creating multiple objects and displaying the static property.
Write a class BankAccount with a static property $totalAccounts and a static method newAccount() that increments $totalAccounts. Call the method without creating any object.
Create a class Employee with a static property $employeeCount and a constructor that increments this property every time a new employee object is created. Display the total count after creating multiple objects.
Write a parent class ParentClass with a static property $value = 50 and a child class ChildClass that overrides $value = 100. Print both values to demonstrate inheritance and overriding.
Create a class Settings with a static property $appName = "MyApp" and a static method getAppName() that returns this value. Call the method without creating an object.
Write a class Product with a static property $totalProducts. Increment this property in the constructor every time a new product is added. Display the total products count.
Create a class Logger with a static property $logs as an array and a static method addLog($message) to add messages. Print the array after adding multiple log messages.
Write a class Game with a static property $playersOnline. Create multiple objects representing players and increment $playersOnline each time. Display total players online.
Create a class Config with a static property $settings as an associative array and a static method get($key) to retrieve values. Add at least three settings and test retrieving them.
Write a class Vehicle with a static property $totalVehicles. Increment it in the constructor and display the value after creating multiple instances of different vehicle types.