MeshWorld India LogoMeshWorld.

PHP self:: vs static:: Explained: A Simple Guide

Listen to ArticleAI Speech
~3 min read narration
100%
PHP self:: vs static:: Explained: A Simple Guide

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

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive