What is $this?

Erland Muchasaj
3 min readMay 12, 2023

--

What is $this in OOP
What is this in OOP

In object-oriented programming with PHP, there are several keywords that refer to different scopes and contexts within a class. Among these keywords, self, static, parent, and $this play crucial roles.
However, they have distinct meanings and are used in different scenarios. This article aims to clarify the differences between these keywords and provide simple examples to illustrate their usage.

So, What is this 🤔?

In PHP, $this is a special keyword that refers to the current instance of a class. It is primarily used within object-oriented programming to access non-static properties, invoke non-static methods, and refer to the current object's context.

When you create an object from a class, $this is used to refer to the specific instance of that class. It provides a way to access and manipulate the object's own data and behavior.

We won't go into much detail on what’s the difference between static and non-static methods and properties in PHP, maybe that's a great name for my next article 😅.

But what you need to know/remember is that $this is only available within the scope of non-static methods and cannot be used in static methods or outside of a class context, while self, static, and parent are used in static methods/properties.

Let's see some examples that illustrate the usage of $this in PHP OOP:

class ParentClass
{
private string $message = "Hello from ParentClass";

public function getMessage(): void
{
return $this->message;
}
}

$myObject = new ParentClass();
echo $myObject->getMessage(); // Output: Hello from ParentClass

In the above example, $this->message is used within the getMessage() method to access the private property message of the current object ($myObject). By using $this, we can retrieve and output the specific message associated with that particular instance.

What about polymorphic inheritance?

In polymorphic inheritance, the concept of $this remains the same as in regular inheritance within object-oriented programming. $this refers to the current instance of the class that is being invoked, regardless of whether it's involved in polymorphic behavior or not (regardless of whether it is a parent or child class).

class ParentClass
{
private string $message = "Hello from ParentClass";

public function getMessage(): string
{
return $this->message;
}
}

class ChildClass extends ParentClass
{
protected string $message = "Hello from ChildClass";

public function getMessage(): string
{
return $this->message;
}
}

$myObject = new ChildClass();
echo $myObject->getMessage(); // Output: Hello from ChildClass

Keep in mind that as we said, this refers to the current class context so doing the following code will have a different result:

class ParentClass
{
private string $message = "Hello from ParentClass";

public function getMessage(): string
{
return $this->message;
}
}

class ChildClass extends ParentClass
{
protected string $message = "Hello from ChildClass";
}

$myObject = new ChildClass();
echo $myObject->getMessage(); // Output: Hello from ParentClass
class ParentClass
{
private string $message = "Hello from ParentClass";

public function getMessage(): string
{
return $this->message;
}
}

class ChildClass extends ParentClass
{
protected string $message = "Hello from ChildClass";

public function getMessage(): string
{
return parent::getMessage();
}
}

$myObject = new ChildClass();
echo $myObject->getMessage(); // Output: Hello from ParentClass

Another use-case for $this is that it can be used for method chaining as well, where multiple methods are invoked in sequence on the same object. This allows for a more concise and fluid coding style.

Method chaining is achieved by returning $this from each method, which allows subsequent method calls to be chained together.

class LadderClass
{
private int $step;

public function __construct(int $initialValue = 0) {
$this->step = $initialValue;
}

public function up(int $value = 1): static
{
$this->step += $value;
return $this;
}

public function down(int $value = 1): static
{
$this->step -= $value;
return $this;
}

public function showStep(): int
{
return $this->step;
}
}

$ladder = new LadderClass();

$ladder->up()->up()->up()->down()->showStep();// Output: 2

Conclusion

$this in PHP OOP refers to the current instance of a class and allows you to access its properties and methods within its own scope. It is a fundamental mechanism for working with objects and their data in an object-oriented manner.

Feel free to Subscribe for more content like this 🔔, clap 👏🏻 , comment 💬 and share the article with anyone you’d like

And as it always has been, I appreciate your support, and thanks for reading.

--

--