Methods and $this keyword
📖 Scenario: Imagine you are creating a simple program to manage a car's details. You want to store the car's brand and model, and then show this information using a method inside a class.
🎯 Goal: You will build a PHP class called Car with properties for brand and model. Then, you will add a method called displayInfo that uses the $this keyword to show the car's brand and model.
📋 What You'll Learn
Create a class named
Car with two public properties: brand and modelCreate an object of the
Car classAssign the values
"Toyota" to brand and "Corolla" to modelAdd a method called
displayInfo inside the Car class that prints the brand and model using $thisCall the
displayInfo method on the object and print the output💡 Why This Matters
🌍 Real World
Classes and methods with <code>$this</code> are used to organize data and behavior in real-world PHP applications like websites and tools.
💼 Career
Understanding how to use classes and the <code>$this</code> keyword is essential for PHP developers building maintainable and reusable code.
Progress0 / 4 steps