Q1
Free (All Users)
What is inheritance in PHP and how does it work?
Inheritance is a fundamental OOP concept where a class (child) can inherit properties and methods from another class (parent) using the extends keyword.
class Animal {
public string $name;
public function speak(): string {
return "...";
}
}
class Dog extends Animal {
public function speak(): string {
return "Woof!";
}
}PHP supports single inheritance only – a class can extend only one parent class.