0
0
PhpComparisonBeginner · 4 min read

Static vs Non Static in PHP: Key Differences and Usage

In PHP, 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.

AspectStaticNon-Static
Belongs toClass itselfInstance (object)
Accessed byClassName::method()$object->method()
Shared across instances?YesNo
Can access $this?NoYes
Use caseUtility functions, constants, shared dataObject-specific behavior and data
Memory usageSingle copy per classSeparate 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
<?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";
?>
Output
Hello, my name is Alice. Total objects: 1
↔️

Non-Static Equivalent

php
<?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";
?>
Output
Hello, my name is Alice. Count from object: 1
🎯

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

Static members belong to the class and can be accessed without creating an object.
Non-static members belong to individual objects and require an instance to use.
Use static for shared data or utility methods, non-static for object-specific data.
Static methods cannot use $this, but non-static methods can.
Choosing between static and non-static depends on whether data should be shared or unique per object.