📖 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
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
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
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
Hint
Use $myCar = new Car(); and print($myCar->getDetails());.
Practice
(1/5)
1. What keyword is used in PHP to create a child class that inherits from a parent class?
easy
A. extends
B. inherits
C. implements
D. uses
Solution
Step 1: Understand class inheritance in PHP
PHP uses a specific keyword to let one class inherit properties and methods from another.
Step 2: Identify the correct keyword
The keyword to extend a class in PHP is extends, not inherits, implements, or uses.
Final Answer:
extends -> Option A
Quick Check:
Keyword for inheritance = extends [OK]
Hint: Remember: PHP uses 'extends' to inherit classes [OK]
Common Mistakes:
Using 'inherits' instead of 'extends'
Confusing 'implements' with 'extends'
Using 'uses' keyword incorrectly
2. Which of the following is the correct syntax to extend a class named Animal in PHP?
easy
A. class Dog extends Animal {}
B. class Dog inherits Animal {}
C. class Dog implements Animal {}
D. class Dog use Animal {}
Solution
Step 1: Recall PHP class extension syntax
To create a child class, PHP uses the syntax: class Child extends Parent {}.
Step 2: Match the syntax with options
class Dog extends Animal {} matches the correct syntax. The other options use incorrect keywords.
Final Answer:
class Dog extends Animal {} -> Option A
Quick Check:
Correct syntax uses 'extends' keyword [OK]
Hint: Use 'extends' keyword exactly after class name [OK]
Common Mistakes:
Using 'inherits' instead of 'extends'
Confusing 'implements' with 'extends'
Using 'use' keyword for inheritance
3. What will be the output of the following PHP code?
class ParentClass {
public function greet() {
return "Hello from Parent";
}
}
class ChildClass extends ParentClass {
public function greet() {
return "Hello from Child";
}
}
$object = new ChildClass();
echo $object->greet();
medium
A. Hello from Parent
B. Fatal error
C. Hello from Child
D. No output
Solution
Step 1: Understand method overriding in child class
The child class ChildClass overrides the greet() method of the parent class.
Step 2: Determine which method is called
When calling greet() on an instance of ChildClass, the overridden method in the child class runs.
Final Answer:
Hello from Child -> Option C
Quick Check:
Child method overrides parent method [OK]
Hint: Child method overrides parent method with same name [OK]
Common Mistakes:
Assuming parent method runs instead of child
Expecting syntax error due to override
Thinking no output will be printed
4. Identify the error in this PHP code that tries to extend a class:
class Vehicle {
public function start() {
return "Starting vehicle";
}
}
class Car Vehicle {
public function start() {
return "Starting car";
}
}
medium
A. Class Car should not have methods
B. Method start() cannot be overridden
C. Class Vehicle should be abstract
D. Missing 'extends' keyword before Vehicle
Solution
Step 1: Check class extension syntax
In PHP, to extend a class, the child class must use the extends keyword.
Step 2: Identify the missing keyword
The code class Car Vehicle {} misses extends between Car and Vehicle.
Final Answer:
Missing 'extends' keyword before Vehicle -> Option D
Quick Check:
Extending requires 'extends' keyword [OK]
Hint: Always write 'extends' when inheriting a class [OK]
Common Mistakes:
Omitting 'extends' keyword
Thinking methods cannot be overridden
Confusing abstract class requirement
5. Given the following classes, what will be the output?
class Base {
public function message() {
return "Base message";
}
}
class Derived extends Base {
public function message() {
return parent::message() . " and Derived message";
}
}
class MoreDerived extends Derived {
public function message() {
return parent::message() . " plus MoreDerived message";
}
}
$obj = new MoreDerived();
echo $obj->message();
hard
A. MoreDerived message
B. Base message and Derived message plus MoreDerived message
C. Base message
D. Fatal error due to parent call
Solution
Step 1: Trace method calls through inheritance chain
MoreDerived::message() calls parent::message(), which is Derived::message().
Step 2: Understand Derived::message() call
Derived::message() calls parent::message(), which is Base::message(), returning "Base message".
Step 3: Combine returned strings
Base returns "Base message"; Derived appends " and Derived message"; MoreDerived appends " plus MoreDerived message".
Final Answer:
Base message and Derived message plus MoreDerived message -> Option B
Quick Check:
parent:: calls chain methods up inheritance [OK]
Hint: parent:: calls chain up inheritance to combine outputs [OK]
Common Mistakes:
Ignoring parent:: calls and output only last method
Expecting fatal error from parent calls
Thinking only base method runs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.