Understanding `self::` vs `static::` in PHP: A Simple Guide for Everyone! ๐Ÿš€

Hey there! ๐Ÿ‘‹ Today, weโ€™re going to learn about two special ways to talk about things in PHP, a programming language. These two ways are called self:: and static::. They help us use properties and methods in classes. Donโ€™t worry if you donโ€™t know what those words mean yet; weโ€™ll explain everything with fun examples! ๐ŸŽ‰

What is a Class? ๐Ÿซ

Before we dive into self:: and static::, letโ€™s understand what a class is. Think of a class like a blueprint for a toy. If you want to build a toy car, you need a blueprint that tells you how to make it. In programming, a class is a blueprint for creating objects (like our toy car).

Hereโ€™s a simple class called Car:

class Car {
    public $color; // This is a property

    public function drive() { // This is a method
        echo "The car is driving! ๐Ÿš—";
    }
}

Now, letโ€™s create a car using this class:

$myCar = new Car();
$myCar->color = "red"; // Setting the color of the car
$myCar->drive(); // Calling the drive method

What is self::? ๐Ÿง‘โ€๐Ÿซ

self:: is like saying, โ€œHey, I want to use something from the class Iโ€™m in right now!โ€ It always refers to the class where it was defined, no matter what.

Letโ€™s see an example:

class Animal {
    protected static $type = "Mammal ๐Ÿพ"; // A property

    public static function getType() { // A method
        return self::$type; // Using self:: to access the property
    }
}

echo Animal::getType(); // Outputs: Mammal ๐Ÿพ

In this example, we have a class called Animal. We have a property called $type and a method called getType(). When we call Animal::getType(), it returns "Mammal ๐Ÿพ" because we used self:: to refer to the property in the same class. ๐Ÿพ

What is static::? ๐ŸŒŸ

Now, static:: is a bit different. Itโ€™s like saying, โ€œI want to use something from the class that is actually being called!โ€ This means it can look at the child classes too!

Letโ€™s see how it works:

class Animal {
    protected static $type = "Mammal ๐Ÿพ"; // A property

    public static function getType() { // A method
        return static::$type; // Using static:: to access the property
    }
}

class Dog extends Animal { // Dog is a child class
    protected static $type = "Dog ๐Ÿถ"; // Overriding the type
}

echo Dog::getType(); // Outputs: Dog ๐Ÿถ

In this example, we have a class called Dog that extends Animal. When we call Dog::getType(), it returns "Dog ๐Ÿถ" because we used static::, which looks at the class that is actually being called (in this case, Dog). ๐Ÿถ

Summary: When to Use Which? ๐Ÿค”

  • Use self:: when you want to refer to something in the class you are currently in, no matter what.
  • Use static:: when you want to refer to something in the class that is actually being called, which can include child classes.

Quick Recap with Emojis! ๐ŸŽˆ

  • self:: = โ€œIโ€™m in this class!โ€ ๐Ÿ 
  • static:: = โ€œIโ€™m in the class thatโ€™s being called!โ€ ๐ŸŒ

Conclusion ๐ŸŽ‰

Now you know the difference between self:: and static:: in PHP! You can think of them as different ways to refer to properties and methods in classes. Just remember, self:: is for the class youโ€™re in, and static:: is for the class thatโ€™s actually being called.

Keep practicing, and soon youโ€™ll be a PHP pro! Happy coding! ๐Ÿ’ปโœจ

Keep helping and happy ๐Ÿ˜„ coding

Vishnu Damwala
Vishnu Damwala

A web geek, an industry experienced web developer & tutor/instructor residing in India ๐Ÿ‡ฎ๐Ÿ‡ณ