PHP - Static Properties

Hello there, aspiring programmers! Today, we're going to dive into the exciting world of PHP static properties. Don't worry if you're new to programming – I'll guide you through every step with the same patience I've used in my classroom for years. Let's get started!

PHP - Static Properties

What Are Static Properties?

Before we jump into the code, let's understand what static properties are. Imagine you're in a classroom (just like mine!), and there's a whiteboard that everyone can see and write on. A static property is like that whiteboard – it belongs to the class itself, not to any particular student (or object, in programming terms).

Key Characteristics of Static Properties:

  1. They are shared among all instances of a class.
  2. They can be accessed without creating an object of the class.
  3. They are declared using the static keyword.

Now, let's see how this works in practice!

Example of Static Properties

Let's create a simple class called Counter to demonstrate static properties:

class Counter {
    public static $count = 0;

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

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

Let's break this down:

  1. We declare a static property $count and initialize it to 0.
  2. The increment() method increases the count by 1.
  3. The getCount() method returns the current count.

Now, let's use this class:

echo Counter::$count; // Outputs: 0

$counter1 = new Counter();
$counter1->increment();
echo Counter::$count; // Outputs: 1

$counter2 = new Counter();
$counter2->increment();
echo Counter::$count; // Outputs: 2

echo Counter::getCount(); // Outputs: 2

Isn't that cool? No matter how many Counter objects we create, they all share the same $count. It's like all the students in my class sharing one whiteboard counter!

The "self" Keyword

You might have noticed we used self::$count in our class. The self keyword is like saying "this class" – it refers to the current class. It's particularly useful with static properties and methods.

Let's expand our Counter class to see more uses of self:

class Counter {
    public static $count = 0;
    private static $secretCount = 100;

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

    private static function incrementSecretCount() {
        self::$secretCount++;
    }

    public static function getSecretCount() {
        return self::$secretCount;
    }
}

Here, we've added a $secretCount that's private (shh, it's a secret!). We use self to access both static properties and methods within the class.

The "parent" Keyword

Now, let's talk about inheritance and the parent keyword. Imagine you're learning to cook from your parent. You might use some of their recipes (inherited methods) but add your own twist. In PHP, parent lets you access methods and properties from the parent class.

Let's create a SuperCounter that extends our Counter:

class SuperCounter extends Counter {
    public function superIncrement() {
        parent::increment();
        parent::increment();
        self::$count++; // We can still use self for the inherited static property
    }
}

Using our new SuperCounter:

$superCounter = new SuperCounter();
$superCounter->superIncrement();
echo SuperCounter::$count; // Outputs: 3

The superIncrement() method calls the parent's increment() twice and then increments once more, resulting in a total increase of 3.

Static Methods Table

Let's summarize the static-related methods we've seen in a handy table:

Method Description
self::$property Accesses a static property within the same class
self::method() Calls a static method within the same class
ClassName::$property Accesses a static property from outside the class
ClassName::method() Calls a static method from outside the class
parent::method() Calls a parent class's method in a child class

Conclusion

And there you have it, friends! We've explored the world of static properties in PHP, from basic usage to inheritance. Remember, static properties are like that classroom whiteboard – shared by all and accessible to everyone who knows the class name.

As with any tool in programming, use static properties wisely. They're great for counters, configuration settings, or any data that should be shared across all instances of a class. But overusing them can lead to code that's harder to manage and test.

Keep practicing, and soon you'll be using static properties like a pro! And remember, in programming as in my classroom, there are no silly questions – only opportunities to learn. Happy coding!

Credits: Image by storyset