PHP › OOP
What is the difference between a static property or method and an instance property or method in PHP?
IntermediateAnswer
A static property or method belongs to the class itself rather than to any specific instance, meaning it is accessed using the class name and scope resolution operator rather than through an object instance, and its value is shared across the entire application rather than being unique to each individual object, while an instance property or method requires creating an actual object and each instance maintains its own independent copy of instance property values.
class Counter {
private static int $count = 0;
public static function increment(): void {
self::$count++;
}
}
Counter::increment();Real-world example A utility class tracking how many database queries have been executed throughout the entire request uses a static property, since that count needs to be shared globally rather than tracked separately per individual object instance.