Hello! Welcome to the fourth lesson in our module on Object-Oriented PHP Fundamentals.
In our last lesson, we explored visibility modifiers (public, private, and protected) and the principle of encapsulation. We established that it's best practice to make properties private to protect them from being changed unexpectedly. This raises a crucial question: if a property is private, how does an object work with its own data?
The answer is the $this keyword. Today, we'll focus entirely on this special variable. You will learn how to use $this to allow an object to refer to itself, enabling it to access its own properties and call its own methods. This is the mechanism that makes encapsulation work and is fundamental to how objects manage their own state.
1. What is $this? The Object's Self-Reference
In simple terms, $this is a special variable that refers to the current object instance.
When you write a class, you're creating a blueprint. You might have a property like $name, but it doesn't have a value yet. It's only when you create an object from that class (e.g., $user1 = new User();) that the blueprint comes to life. Inside the $user1 object, $this is a direct reference to $user1. If you create another object, $user2, then inside that object, $this refers to $user2.
Think of it like the pronoun "I". When you say, "I am hungry," you are referring to yourself. When an object uses $this, it's referring to itself.

Let's watch a short video that explains this concept.
The video '$this Keyword' by Dino Cajic starts with a great analogy to help you understand the purpose of $this.
Watch the first 50 seconds of the video. The presenter explains why $this is necessary for an object to reference itself.
2. Accessing and Modifying Properties
The most common use of $this is to access and modify an object's properties from within its methods. This is how an object manages its internal state. The syntax is $this->propertyName, without the dollar sign ($) on the property name itself.
This is the key to working with the private and protected properties we discussed in the last lesson. While outside code cannot touch them, the object's own methods can.
Accessing (Reading) Properties
A method can use $this to read the value of a property and use it in calculations, logic, or just return it. This is the principle behind "getter" methods.
class User
{
private $username = "DefaultUser";
// This public method can access the private property
public function getUsername()
{
// Use $this to refer to this object's 'username' property
return $this->username;
}
}
$user = new User();
// echo $user->username; // This would cause a Fatal error because $username is private
// But this works perfectly!
echo $user->getUsername(); // Outputs: DefaultUser
Modifying (Writing) Properties
Similarly, a method can use $this to change the value of a property. This is how an object updates its state based on actions. This is the principle behind "setter" methods.
Let's look at a practical example where methods use $this to both check and change an object's state.
This segment from Dino Cajic's video demonstrates how to use $this to change a property's value within a method. It uses a Car class with a carOn property.
Watch from 08:31 to 11:00. Notice how the turnOn() and turnOff() methods first read the value of $this->carOn to see the car's current state, and then write a new value to $this->carOn to change that state.
The article this video is based on provides a great textual walkthrough of these concepts, which you can use for review.
The article 'PHP $this keyword' by Dino Cajic provides the full code for the Car class example. It's a great reference to see how $this is used to manage state.
First, read the section that begins with the GermanShepherd class to see a simple getter example with $this->eye_color. Then, scroll down to the Car class example and read the section that shows the turnOn() and turnOff() methods. This reinforces what you just saw in the video.
3. Calling Other Methods
The $this keyword isn't limited to properties. You can also use it to call another method within the same object. This is extremely useful for breaking down complex tasks into smaller, reusable helper methods and keeping your code organized (following the DRY - Don't Repeat Yourself - principle).
The syntax is very similar: $this->methodName().
Imagine our Car class needs to print the make and model in several different messages. Instead of writing $this->make . " " . $this->model every time, we can create one helper method and call it wherever we need it.
class Car
{
public $make;
public $model;
private $car_on = false;
// A helper method
private function getMakeAndModel()
{
return $this->make . " " . $this->model;
}
public function turnOn()
{
if ($this->car_on) {
// Here, we call another method on the same object
return "The " . $this->getMakeAndModel() . " is already on.";
}
$this->car_on = true;
// And we call it again here
return $this->getMakeAndModel() . " has been turned on.";
}
}
$subaru = new Car();
$subaru->make = "Subaru";
$subaru->model = "WRX STI";
echo $subaru->turnOn(); // Outputs: Subaru WRX STI has been turned on.
Notice that we can call a private method (getMakeAndModel) from a public method (turnOn) using $this. This is a powerful pattern for organizing your internal class logic.
Let's see this refactoring process in action.
In this final clip from Dino Cajic's video, he demonstrates how to create a helper method and call it using $this to reduce repetitive code.
Watch from 14:36 to 16:26. Pay attention to how he creates getMakeModel() and then replaces the repeated string concatenation with a simple call: $this->getMakeModel().
Conclusion
Great job! You've now mastered one of the most essential concepts in PHP OOP. The $this keyword is the glue that holds an object together, allowing it to be a self-contained, functional unit that manages its own data and behavior.
Key Takeaways:
$thisis a special variable that always refers to the current object instance.- It is used inside a class's methods to access that class's own properties (
$this->property) and methods ($this->method()). - Using
$thisis essential for implementing encapsulation, as it allows methods to operate onprivateandprotectedmembers. - It helps in writing clean, organized code by allowing you to call reusable helper methods within the same class.
Preview of the Next Lesson:
So far, we've been creating an object and then setting its properties one by one, like $subaru->make = "Subaru". There is a much more efficient and robust way to set up an object's initial state. In the next lesson, we will learn about constructors and destructors. Constructors are special methods that run automatically when an object is created, and they frequently use $this to assign initial values to properties right away.