PHP $this, self and parent operators

 ← Dev Articles
👍 0
👎 0

Sometimes different aspects of object oriented programming can be a little confusing if you can’t picture a use case for them.  Three class operators in PHP where usage can be a little confusing at times are $this, self and parent.  In this article I will try to break things down and maybe you can see where you can use this in your code.   Ok so let’s begin.

 

1. $this

- Refers to the current object instance.

- You use $this when you want to access properties or methods of the current object.


Example:

 

 class Animal {

    public $name;

 

    public function setName($name) {

        $this->name = $name;   // "this object’s" name

    }

 

    public function getName() {

        return $this->name;    // returns "this object’s" name

    }

 }

 

 $dog = new Animal();

 $dog->setName("Buddy");

 

 echo $dog->getName(); // Output: Buddy

 

 

2. self

- Refers to the current class itself, not the instance.
- Used for static methods and static properties.
- Does not depend on an object ($this is not available in static context).


Example:

 

 class MathHelper {

    public static $pi = 3.14159;

 

    public static function circleArea($radius) {

        return self::$pi * $radius * $radius; // accessing static property with self

    }

 }

 

 echo MathHelper::circleArea(5); // Output: 78.53975

 

 

3. parent

- Refers to the immediate parent class.
- Used when you want to access a method or constructor from the parent class that is overridden in the child.

Example:

 

 class Animal {

    public function makeSound() {

        return "Some generic animal sound";

    }

 }

 

 class Dog extends Animal {

    public function makeSound() {

        // Call parent method, then add more

        return parent::makeSound() . " and Woof!";

    }

 }

 

 $dog = new Dog();

 echo $dog->makeSound(); // Output: Some generic animal sound and Woof!

 

 

 

Now to summarize:

- $this refers to an instance of the class. It isn’t available in a static context, therefore cannot be used within a static class function.

- The self:: operator refers to the class-level property. It is used in a static context and refers to the actual class itself. 

- The parent:: operator calls the overridden method from the parent class. It’s used in inheritance typically to call an overwritten method on the parent class.

 

Here is a really good example that should help you concieve these concepts and clear up any confusion.

 

 

 class Animal {

    public $name;

    public static $kingdom = "Animalia";

 

    public function __construct($name) {

        $this->name = $name; // instance reference

    }

 

    public function describe() {

        return "I am an animal named {$this->name}.";

    }

 

    public static function getKingdom() {

        return "Kingdom: " . self::$kingdom; // static reference

    }

 }

 

 class Dog extends Animal {

    public function describe() {

        // Use parent to get base description

        $base = parent::describe();

 

        // Add Dog-specific description

        return $base . " I am also a dog that says Woof!";

    }

 

    public function introduce() {

        // `$this` calls instance method

        return $this->describe();

    }

 

    public static function getInfo() {

        // `self` calls static property from this class (or parent if not overridden)

        return "Dogs belong to " . self::$kingdom;

    }

 }

 

 // ------------------- USAGE -------------------

 

 // Create an object

 $dog = new Dog("Buddy");

 

 // $this -> instance reference

 echo $dog->introduce();

 // Output: I am an animal named Buddy. I am also a dog that says Woof!

 

 echo "<br>";

 

 // self -> static reference

 echo Dog::getInfo();

 // Output: Dogs belong to Animalia

 

 echo "<br>";

 

 // parent -> calling parent method inside child

 echo $dog->describe();

 // Output: I am an animal named Buddy. I am also a dog that says Woof!

 

 echo "<br>";

 

 // static method from parent

 echo Animal::getKingdom();

 // Output: Kingdom: Animalia