When should composition be preferred over inheritance in PHP class design?

Composition should be preferred over inheritance in PHP class design when there is a "has-a" relationship between classes rather than an "is-a" relationship. This means that one class contains an instance of another class as a member, rather than inheriting behavior from a parent class. This approach promotes better code reusability, flexibility, and maintainability.

class Engine {
    public function start() {
        echo "Engine started";
    }
}

class Car {
    private $engine;

    public function __construct() {
        $this->engine = new Engine();
    }

    public function startCar() {
        $this->engine->start();
        echo "Car started";
    }
}

$car = new Car();
$car->startCar();