📖 Scenario: Imagine you are building a simple system to manage different types of vehicles. You want to reuse some common behavior from a base vehicle class and extend it in child classes.
🎯 Goal: You will create a base class with a method, then create a child class that overrides this method but still calls the original method using the parent keyword.
📋 What You'll Learn
Create a base class called Vehicle with a method describe() that returns the string "This is a vehicle."
Create a child class called Car that extends Vehicle and overrides the describe() method.
Inside the Car class's describe() method, call the parent describe() method using the parent keyword and append the string " Specifically, it is a car."
Create an instance of the Car class and print the result of calling its describe() method.
💡 Why This Matters
🌍 Real World
Using inheritance and the parent keyword helps programmers reuse code and extend functionality without rewriting common parts.
💼 Career
Understanding how to use the parent keyword is essential for working with object-oriented PHP code in many real-world projects, such as web applications and frameworks.
Progress0 / 4 steps
1
Create the base class Vehicle
Create a class called Vehicle with a public method describe() that returns the string "This is a vehicle."
PHP
Hint
Use class Vehicle {} to create the class and define the method inside it.
2
Create the child class Car
Create a class called Car that extends Vehicle and overrides the describe() method.
PHP
Hint
Use class Car extends Vehicle {} and define describe() inside it.
3
Use parent keyword inside Car's describe method
Inside the Car class's describe() method, call the parent describe() method using parent::describe() and append the string " Specifically, it is a car." to the returned value.
PHP
Hint
Use parent::describe() to call the base class method inside the child method.
4
Create Car instance and print description
Create an instance of the Car class called $myCar and print the result of calling its describe() method using echo.
PHP
Hint
Use $myCar = new Car(); and echo $myCar->describe(); to show the message.
Practice
(1/5)
1. What does the parent keyword do in PHP classes?
easy
A. It renames the parent class.
B. It creates a new instance of the parent class.
C. It deletes the parent class methods from the child class.
D. It allows a child class to access methods or properties from its parent class.
Solution
Step 1: Understand the role of parent keyword
The parent keyword in PHP is used inside a child class to refer to its parent class.
Step 2: Identify what parent allows
It allows the child class to call or extend methods or properties defined in the parent class.
Final Answer:
It allows a child class to access methods or properties from its parent class. -> Option D
Quick Check:
parent accesses parent methods/properties [OK]
Hint: Remember: parent calls parent class features [OK]
Common Mistakes:
Thinking parent creates new objects
Confusing parent with $this
Believing parent deletes methods
2. Which of the following is the correct syntax to call a parent class method named show inside a child class method?
easy
A. parent.show();
B. parent->show();
C. parent::show();
D. parent:show();
Solution
Step 1: Recall PHP syntax for calling parent methods
In PHP, to call a parent class method inside a child, use the scope resolution operator :: with parent.
Step 2: Match the correct syntax
The correct syntax is parent::methodName();, so for method show, it is parent::show();.
Final Answer:
parent::show(); -> Option C
Quick Check:
Use :: with parent to call methods [OK]
Hint: Use double colon (::) to call parent methods [OK]
Common Mistakes:
Using arrow (->) instead of double colon (::)
Using dot (.) operator which is for strings
Using single colon (:) which is invalid syntax
3. What will be the output of this PHP code?
class A {
public function greet() {
return "Hello from A";
}
}
class B extends A {
public function greet() {
return parent::greet() . " and B";
}
}
$b = new B();
echo $b->greet();
medium
A. Hello from A
B. Hello from A and B
C. Hello from B
D. Error: Cannot call parent method
Solution
Step 1: Understand method overriding and parent call
Class B overrides greet() but calls parent::greet() which returns "Hello from A".
Step 2: Concatenate parent result with " and B"
The child method returns the parent's string plus " and B", so the full string is "Hello from A and B".
Final Answer:
Hello from A and B -> Option B
Quick Check:
parent::greet() + " and B" = "Hello from A and B" [OK]
Hint: parent::method() returns parent's output, can concatenate [OK]
Common Mistakes:
Ignoring parent:: call and expecting only child's string
Thinking it causes an error
Confusing output with only parent's or only child's string
4. Identify the error in this PHP code:
class ParentClass {
public function sayHi() {
echo "Hi from parent";
}
}
class ChildClass extends ParentClass {
public function sayHi() {
parent.sayHi();
echo " and child";
}
}
$obj = new ChildClass();
$obj->sayHi();
medium
A. Using dot (.) instead of scope resolution (::) to call parent method
B. Missing parentheses after method name
C. Child class cannot override parent methods
D. Parent class method is private
Solution
Step 1: Check how parent method is called
The code uses parent.sayHi(); which is invalid syntax in PHP.
It should be parent::sayHi(); to call the parent method inside child.
Final Answer:
Using dot (.) instead of scope resolution (::) to call parent method -> Option A
Quick Check:
Use parent::method(), not parent.method() [OK]
Hint: Use ::, not . to call parent methods [OK]
Common Mistakes:
Using dot (.) operator for method calls
Forgetting parentheses after method name
Assuming private methods can be called by child
5. Given these classes, what will be the output?
class Base {
protected function message() {
return "Base message";
}
}
class Derived extends Base {
public function message() {
return parent::message() . " extended";
}
}
class MoreDerived extends Derived {
public function message() {
return parent::message() . " and more";
}
}
$obj = new MoreDerived();
echo $obj->message();
hard
A. Base message extended and more
B. Base message and more
C. Error: Cannot access protected method
D. Base message extended
Solution
Step 1: Trace method calls through inheritance
MoreDerived::message() calls parent::message() which is Derived::message().
Step 2: Derived::message() calls Base::message()
Derived calls parent::message() which is Base::message() returning "Base message" and appends " extended".
Step 3: Combine all returned strings
So Derived returns "Base message extended" and MoreDerived appends " and more" resulting in "Base message extended and more".
Final Answer:
Base message extended and more -> Option A
Quick Check:
Chained parent calls build full string [OK]
Hint: Each parent:: call adds to string stepwise [OK]
Common Mistakes:
Expecting error due to protected method (allowed in child)
Ignoring chained parent calls
Assuming only base message prints
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.