0
0
PHPprogramming~10 mins

Methods and $this keyword in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Methods and $this keyword
Create Object
Call Method
Inside Method
$this refers to Current Object
Access/Modify Object Properties
Return or Output Result
End Method Execution
This flow shows how calling a method on an object uses $this to access that object's properties inside the method.
Execution Sample
PHP
<?php
class Car {
  public $color;
  public function setColor($c) {
    $this->color = $c;
  }
}
$car = new Car();
$car->setColor('red');
echo $car->color;
?>
This code creates a Car object, sets its color using a method with $this, then prints the color.
Execution Table
StepActionEvaluationResult
1Create new Car object$car = new Car()Object $car created with property color = null
2Call method setColor with argument 'red'$car->setColor('red')Inside setColor, $this refers to $car
3Assign $this->color = 'red'$this->color = 'red'Property color of $car set to 'red'
4Method setColor endsReturn voidBack to main code
5Echo $car->colorecho $car->colorOutputs: red
💡 Method completes and program outputs the color property value.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
$car->colornullnull'red''red'
$thisN/ARefers to $carRefers to $carRefers to $car
Key Moments - 2 Insights
Why do we use $this->color inside the method instead of just $color?
Inside the method, $this->color means the color property of the current object. Just $color would be a local variable, not the object's property. See step 3 in execution_table where $this->color is assigned.
What does $this refer to inside the method?
$this always refers to the object that called the method. In step 2 and 3, $this points to $car, so changes affect $car's properties.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $car->color after step 1?
Anull
B'red'
Cundefined
D0
💡 Hint
Check variable_tracker row for $car->color after Step 1.
At which step does $this->color get assigned the value 'red'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the Action and Evaluation columns in execution_table for assignment.
If we called setColor('blue') instead, what would be the output at step 5?
Ared
Bnull
Cblue
DError
💡 Hint
Changing the argument changes the property value assigned in step 3.
Concept Snapshot
Methods are functions inside classes.
Use $this inside methods to access the current object's properties.
Example: $this->property = value;
Calling a method uses $this to refer to the calling object.
This lets methods change or use object data safely.
Full Transcript
This lesson shows how methods in PHP classes use the $this keyword to access the current object's properties. When you create an object and call its method, inside that method $this points to the object itself. This lets you read or change the object's properties. For example, setting $this->color changes the color property of the object that called the method. The execution table traces creating the object, calling the method, assigning the property, and printing the result. The variable tracker shows how $car->color changes from null to 'red'. Key moments explain why $this is needed and what it means. The quiz checks understanding of $this and property changes. Remember, $this is your way to say "this object" inside its own methods.