What are some potential pitfalls of using inheritance in PHP classes, as seen in the code example provided?
One potential pitfall of using inheritance in PHP classes is the tight coupling it creates between parent and child classes. This can lead to issues such as unintended side effects when modifying the parent class, difficulty in understanding and maintaining code, and limitations on class reusability. To address this, consider using composition over inheritance to achieve code reuse without the drawbacks of tight coupling.
class Animal {
protected $name;
public function __construct($name) {
$this->name = $name;
}
}
class Dog {
private $animal;
public function __construct($name) {
$this->animal = new Animal($name);
}
}