Static vs Non Static in PHP: Key Differences and Usage
static methods and properties belong to the class itself and can be accessed without creating an object, while non-static methods and properties belong to an instance of the class and require an object to be accessed. Static members are shared across all instances, whereas non-static members are unique to each object.Quick Comparison
Here is a quick side-by-side comparison of static and non-static members in PHP.
| Aspect | Static | Non-Static |
|---|---|---|
| Belongs to | Class itself | Instance (object) |
| Accessed by | ClassName::method() | $object->method() |
| Shared across instances? | Yes | No |
Can access $this? | No | Yes |
| Use case | Utility functions, constants, shared data | Object-specific behavior and data |
| Memory usage | Single copy per class | Separate copy per object |
Key Differences
Static methods and properties belong to the class itself, not to any object created from the class. This means you can call a static method or access a static property without creating an instance of the class. Static members are shared among all instances, so changing a static property affects all objects.
On the other hand, non-static methods and properties belong to individual objects. You must create an object to use them, and each object has its own copy of these properties. Non-static methods can use the $this keyword to refer to the current object, which is not possible in static methods.
Static members are useful for functionality that does not depend on object state, like helper functions or shared counters. Non-static members are for data and behavior that vary between objects.
Code Comparison
<?php class Example { public static $count = 0; public $name; public function __construct($name) { $this->name = $name; self::$count++; } public static function getCount() { return self::$count; } public function greet() { return "Hello, my name is {$this->name}."; } } // Using non-static method and property $obj1 = new Example('Alice'); echo $obj1->greet() . "\n"; // Using static method and property echo "Total objects: " . Example::getCount() . "\n"; ?>
Non-Static Equivalent
<?php class Example { public $count = 0; public $name; public function __construct($name) { $this->name = $name; $this->count++; } public function getCount() { return $this->count; } public function greet() { return "Hello, my name is {$this->name}."; } } // Using non-static method and property $obj1 = new Example('Alice'); echo $obj1->greet() . "\n"; // Trying to get count from object echo "Count from object: " . $obj1->getCount() . "\n"; ?>
When to Use Which
Choose static when you need to share data or behavior across all instances or when the functionality does not depend on object state, such as utility functions or counters. Use non-static when each object should maintain its own state and behavior, like user profiles or individual settings. Static members simplify access without creating objects but lack flexibility for instance-specific data.
Key Takeaways
$this, but non-static methods can.