Extending classes lets you create a new class based on an existing one. This helps you reuse code and add new features easily.
Extending classes in PHP
<?php class ParentClass { // properties and methods } class ChildClass extends ParentClass { // new or changed properties and methods } ?>
The extends keyword is used to create a child class.
The child class inherits all public and protected properties and methods from the parent.
Dog that changes the speak method of the parent Animal.<?php class Animal { public function speak() { echo "Animal sound\n"; } } class Dog extends Animal { public function speak() { echo "Bark!\n"; } } $dog = new Dog(); $dog->speak(); ?>
Car class extends Vehicle and adds a new method openDoor.<?php class Vehicle { public function start() { echo "Vehicle started\n"; } } class Car extends Vehicle { public function openDoor() { echo "Door opened\n"; } } $car = new Car(); $car->start(); $car->openDoor(); ?>
This program shows a Student class extending Person. The student has a name and a grade. It can greet and study.
<?php class Person { public $name; public function __construct($name) { $this->name = $name; } public function greet() { echo "Hello, my name is {$this->name}.\n"; } } class Student extends Person { public $grade; public function __construct($name, $grade) { parent::__construct($name); $this->grade = $grade; } public function study() { echo "{$this->name} is studying for grade {$this->grade}.\n"; } } $student = new Student("Alice", 10); $student->greet(); $student->study(); ?>
Use parent::__construct() to call the parent class constructor inside the child.
Child classes can override parent methods by defining a method with the same name.
Only public and protected members are inherited; private members are not accessible directly.
Extending classes helps reuse and organize code by creating child classes from parent classes.
Use extends keyword to make a child class inherit from a parent.
Child classes can add new features or change existing ones easily.
Practice
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 isextends, notinherits,implements, oruses.Final Answer:
extends -> Option AQuick Check:
Keyword for inheritance = extends [OK]
- Using 'inherits' instead of 'extends'
- Confusing 'implements' with 'extends'
- Using 'uses' keyword incorrectly
Animal in PHP?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 AQuick Check:
Correct syntax uses 'extends' keyword [OK]
- Using 'inherits' instead of 'extends'
- Confusing 'implements' with 'extends'
- Using 'use' keyword for inheritance
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();Solution
Step 1: Understand method overriding in child class
The child classChildClassoverrides thegreet()method of the parent class.Step 2: Determine which method is called
When callinggreet()on an instance ofChildClass, the overridden method in the child class runs.Final Answer:
Hello from Child -> Option CQuick Check:
Child method overrides parent method [OK]
- Assuming parent method runs instead of child
- Expecting syntax error due to override
- Thinking no output will be printed
class Vehicle {
public function start() {
return "Starting vehicle";
}
}
class Car Vehicle {
public function start() {
return "Starting car";
}
}Solution
Step 1: Check class extension syntax
In PHP, to extend a class, the child class must use theextendskeyword.Step 2: Identify the missing keyword
The codeclass Car Vehicle {}missesextendsbetweenCarandVehicle.Final Answer:
Missing 'extends' keyword before Vehicle -> Option DQuick Check:
Extending requires 'extends' keyword [OK]
- Omitting 'extends' keyword
- Thinking methods cannot be overridden
- Confusing abstract class requirement
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();Solution
Step 1: Trace method calls through inheritance chain
MoreDerived::message()callsparent::message(), which isDerived::message().Step 2: Understand Derived::message() call
Derived::message()callsparent::message(), which isBase::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 BQuick Check:
parent:: calls chain methods up inheritance [OK]
- Ignoring parent:: calls and output only last method
- Expecting fatal error from parent calls
- Thinking only base method runs
Ready to prove it?
Pass the test and this pattern moves unknown
