PHP : OOP - Static Properties

In PHP, static properties are properties that belong to the class itself rather than to instances (objects) of the class. They are shared among all instances of the class. Static properties are declared using the static keyword inside the class. These properties can be accessed without creating an instance of the class.

Static Property Syntax:


class ClassName {
    public static $staticProperty;
}

 

Accessing Static Properties:

Static properties are accessed using the ClassName::$propertyName syntax, where ClassName is the name of the class containing the static property.


class Math {
    public static $pi = 3.14159;
}

// Accessing a static property
echo "The value of Pi is: " . Math::$pi; // Output: The value of Pi is: 3.14159

 

Setting and Changing Static Properties:

Static properties can be set and changed using the same syntax as accessing them.


class Counter {
    public static $count = 0;

    public static function increment() {
        self::$count++;
    }
}

Counter::$count = 5; // Set static property directly
echo "Count: " . Counter::$count . "<br>"; // Output: Count: 5

Counter::increment(); // Increment the count
echo "Count: " . Counter::$count . "<br>"; // Output: Count: 6

 

Static Properties with Constructor:

Since static properties are associated with the class itself, they are shared among all instances of the class. If you change the static property in one instance, it will affect all other instances as well.


class MyClass {
    public static $sharedProperty = 0;

    public function __construct() {
        self::$sharedProperty++;
    }
}

$obj1 = new MyClass();
$obj2 = new MyClass();

echo "Shared Property: " . MyClass::$sharedProperty; // Output: Shared Property: 2
 

Static vs. Non-Static Properties:

Static Properties:

  •   Belong to the class itself, not to instances.
  •   Are shared among all instances of the class.
  •   Accessed using ClassName::$propertyName.
  •   Useful for data that is common to all instances of a class.

Non-Static Properties:

  •   Belong to individual instances of the class (objects).
  •   Each object has its own copy of non-static properties.
  •   Accessed using $this->propertyName inside non-static methods.
  •   Useful for data specific to each instance of a class.

Static Properties Example:

Here's a more detailed example demonstrating the use of static properties:


class Product {
    public $name;
    public $price;
    public static $count = 0;

    public function __construct($name, $price) {
        $this->name = $name;
        $this->price = $price;
        self::$count++; // Increment the count when a new instance is created
    }

    public static function getCount() {
        return self::$count;
    }
}

// Create some product objects
$product1 = new Product("Phone", 500);
$product2 = new Product("Laptop", 1200);

// Accessing static property
echo "Total products created: " . Product::getCount() . "<br>"; // Output: Total products created: 2

In this example:

  • The Product class has a static property $count to keep track of the number of product instances created.
  • Each time a new Product object is created, the $count is incremented.
  • The getCount() static method returns the value of the $count property.

Use Cases for Static Properties:

  • Counters: To keep track of how many instances of a class have been created.
  • Configuration Values: Constants that should apply to all instances of a class.
  • Utility Data: Data that is common across all instances of a class.

Notes:

  • Static properties are shared among all instances of a class, so changing the property in one instance will affect all others.
  • Static properties can be accessed and modified without creating an instance of the class.
  • They are often used for variables that should be common to all instances of a class, such as counts, configuration settings, or utility data.

Conclusion:

Static properties in PHP allow you to define properties that belong to the class itself rather than to instances of the class. They are shared among all instances and can be accessed and modified without creating an instance of the class. Static properties are useful for keeping track of class-level data that should be common to all instances, such as counters, configuration settings, or utility data.