How can the use of composition instead of inheritance help overcome the limitations of PHP's single inheritance model?

Using composition instead of inheritance in PHP can help overcome the limitations of single inheritance by allowing classes to be composed of multiple objects, each responsible for a specific aspect of functionality. This approach promotes code reuse, modularity, and flexibility, as classes can be easily composed and extended without being constrained by the limitations of single inheritance.

class Logger {
    public function log($message) {
        echo "Logging: " . $message . "\n";
    }
}

class EmailNotifier {
    public function sendNotification($email, $message) {
        echo "Sending email to " . $email . ": " . $message . "\n";
    }
}

class NotificationService {
    private $logger;
    private $notifier;
    
    public function __construct(Logger $logger, EmailNotifier $notifier) {
        $this->logger = $logger;
        $this->notifier = $notifier;
    }
    
    public function notifyUser($email, $message) {
        $this->logger->log("Preparing to notify user");
        $this->notifier->sendNotification($email, $message);
    }
}

$logger = new Logger();
$notifier = new EmailNotifier();
$service = new NotificationService($logger, $notifier);
$service->notifyUser("example@example.com", "Hello, this is a notification!");