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