Demystifying PHP Static and Self Keywords

KolaKachi
This entry is part 7 of 24 in the series PHP Object-Oriented Programming(OOP) Crash Course

Welcome back to another informative session about PHP. In the Post, we’re diving into the fascinating realm of static properties and the self keyword. If you’ve ever been curious about these concepts, you’re in the right place.

Static Properties

First, let’s talk about static properties. When you declare a property as static within a class, it takes on unique characteristics. Unlike regular properties that belong to class instances, static properties belong to the class itself. Think of it like this: if class instances are the products rolling off the assembly line, the class itself is the factory. Changes to a static property affect the entire factory.

class Product {
    public static $total = 0;

    public function calculateTotal() {
        // Accessing a static property
        return self::$total;
    }
}

Static properties can still be public, private, or protected, but combining public with private or protected with public is not allowed.

Accessing Static Properties

Now, let’s see how to access a static property. You can do this either through an instance of the class or directly using the class name. For instance, if we have a class Product with a static property total, we can access it like this:

$product = new Product();
echo $product->total; // This won't work, as $total is static

// To access it properly, use the class name:
echo Product::$total;

This demonstrates that static properties can be accessed both from instances and the class itself. However, it’s best practice to access them using the class name to avoid issues when class names change.

Changing Static Properties

One critical aspect of static properties is that changes are persistent during the script’s execution. While instance properties vary for each object, a static property’s value remains consistent for the entire script. Here’s an example:

$product = new Product();
$product->total = 200;

$anotherProduct = new Product();
echo $anotherProduct->total; // Outputs 200, even for a different instance

// To modify and access the static property:
Product::$total = 500;
echo Product::$total; // Outputs 500

In this example, we set the static property $total to 200 for the first instance. When we create another instance, it still holds the modified value. This demonstrates how changes to static properties persist throughout the script’s execution.

The Self Keyword

To access static properties within a class, you can use the self keyword followed by the scope resolution operator ::. This allows you to refer to the class itself. For example:

class Product {
    public static $total = 0;

    public function calculateTotal() {
        return self::$total;
    }
}

Using self within the class is essential, especially when the class name may change or for clarity in code. It ensures you’re always referring to the class’s context.

Conclusion

Understanding static properties and using the self keyword in PHP can be tremendously beneficial. Static properties persist throughout a script’s execution, providing a way to share and maintain data across all instances of a class. And the self keyword allows you to access these static properties consistently within the class.

With this newfound knowledge, you can write more organized and efficient PHP code. Stay tuned for more PHP insights and tutorials in the future.

Series Navigation<< Understanding Accessors in Object-Oriented ProgrammingUnderstanding Constructors and Their Benefits in PHP >>

Leave a Reply

Your email address will not be published. Required fields are marked *