0
0
PHPprogramming~30 mins

Extending classes in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Extending Classes in PHP
📖 Scenario: You are creating a simple program to manage vehicles. You want to reuse common features of vehicles but also add special features for cars.
🎯 Goal: Build a PHP program that defines a base class Vehicle and a child class Car that extends Vehicle. You will add properties and methods step-by-step and then display the car details.
📋 What You'll Learn
Create a base class called Vehicle with a property $brand.
Create a child class called Car that extends Vehicle.
Add a property $model to the Car class.
Add a method getDetails() in Car that returns a string with brand and model.
Create an object of Car and print the details.
💡 Why This Matters
🌍 Real World
Extending classes helps reuse code and organize related features in programs like vehicle management systems.
💼 Career
Understanding class inheritance is essential for PHP developers building scalable and maintainable applications.
Progress0 / 4 steps
1
Create the base class Vehicle
Write a PHP class called Vehicle with a public property $brand set to the string "Toyota".
PHP
Need a hint?

Use class Vehicle { public string $brand = "Toyota"; } to create the class and property.

2
Create the Car class extending Vehicle
Add a class called Car that extends Vehicle. Inside Car, add a public property $model set to "Corolla".
PHP
Need a hint?

Use class Car extends Vehicle { public string $model = "Corolla"; }.

3
Add getDetails() method to Car
Inside the Car class, add a public method called getDetails() that returns a string combining $this->brand and $this->model separated by a space.
PHP
Need a hint?

Define public function getDetails(): string { return $this->brand . " " . $this->model; }.

4
Create Car object and print details
Create an object called $myCar of class Car. Then print the result of calling $myCar->getDetails().
PHP
Need a hint?

Use $myCar = new Car(); and print($myCar->getDetails());.