Extending a class means creating a new class that inherits properties and methods from an existing class. This helps reuse code and add new features.
Click to reveal answer
beginner
How do you declare a class that extends another class in PHP?
Use the <code>extends</code> keyword. For example: <br><code>class ChildClass extends ParentClass { }</code>
Click to reveal answer
beginner
Can a child class override a method from its parent class?
Yes, a child class can provide its own version of a method defined in the parent class by defining a method with the same name.
Click to reveal answer
intermediate
What keyword allows a child class to call the parent class's method it overrides?
The parent:: keyword is used to call the parent class's method from the child class.
Click to reveal answer
beginner
Why is extending classes useful in programming?
It helps avoid repeating code, makes programs easier to maintain, and allows adding or changing features without rewriting everything.
Click to reveal answer
Which keyword is used to create a class that inherits from another class in PHP?
Auses
Bimplements
Cinherits
Dextends
✗ Incorrect
The extends keyword is used to inherit from a parent class in PHP.
If a child class has a method with the same name as its parent, what happens when you call that method on the child?
AThe parent's method runs
BAn error occurs
CThe child's method runs
DBoth methods run automatically
✗ Incorrect
The child's method overrides the parent's method and runs instead.
How can a child class call the original method from its parent class it has overridden?
AUsing <code>super::method()</code>
BUsing <code>parent::method()</code>
CUsing <code>this::method()</code>
DIt cannot call the parent's method
✗ Incorrect
The parent::method() syntax calls the parent class's method.
Which of these is NOT a benefit of extending classes?
ASlower program execution
BEasier maintenance
CCode reuse
DAdding new features easily
✗ Incorrect
Extending classes usually does not slow down programs; it helps organize code better.
What happens if a child class does not define a method that exists in the parent class?
AThe parent's method is used
BThe method is ignored
CThe program crashes
DYou must define the method in the child
✗ Incorrect
If the child class lacks the method, PHP uses the parent's method automatically.
Explain in your own words what it means to extend a class in PHP and why it is useful.
Think about how a child class can use and change what the parent class has.
You got /5 concepts.
Describe how method overriding works when extending classes in PHP and how to call the parent method from the child.
Focus on what happens when both classes have a method with the same name.
You got /4 concepts.
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.