How do you use inheritance in PHP

Inheritance in PHP is a fundamental concept of object-oriented programming that allows a class to inherit properties and methods from another class. This helps in code reusability and creating a more manageable code structure.

Example of Inheritance in PHP

<?php class Animal { public function speak() { return "The animal makes a sound."; } } class Dog extends Animal { public function speak() { return "The dog barks."; } } class Cat extends Animal { public function speak() { return "The cat meows."; } } $dog = new Dog(); echo $dog->speak(); // Outputs: The dog barks. $cat = new Cat(); echo $cat->speak(); // Outputs: The cat meows. ?>

Inheritance PHP Object-Oriented Programming Code Reusability PHP Classes