How can inheritance be utilized in PHP classes to create specialized subclasses with different behaviors?

Inheritance in PHP classes allows for the creation of specialized subclasses that inherit properties and methods from a parent class, while also having the ability to define their own unique behaviors. This can be useful when you have classes that share common attributes and methods, but also require specific functionalities.

// Parent class
class Animal {
    public $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function makeSound() {
        echo "Animal sound";
    }
}

// Subclass
class Dog extends Animal {
    public function makeSound() {
        echo "Woof!";
    }
}

// Subclass
class Cat extends Animal {
    public function makeSound() {
        echo "Meow!";
    }
}

// Usage
$dog = new Dog("Buddy");
$dog->makeSound(); // Output: Woof!

$cat = new Cat("Whiskers");
$cat->makeSound(); // Output: Meow!