How can PHP developers utilize composition to improve the structure and organization of their classes?

PHP developers can utilize composition by creating smaller, more focused classes that handle specific responsibilities. This can improve the structure and organization of their classes by promoting reusability, maintainability, and flexibility. By composing classes with specific functionalities, developers can easily modify or extend the behavior of their code without affecting other parts of the application.

<?php

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

class User {
    private $logger;

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

    public function createUser($username) {
        $this->logger->log("User created: $username");
    }
}

$logger = new Logger();
$user = new User($logger);
$user->createUser("john_doe");

?>