What are the advantages of using Traits in PHP for flexible class construction?

Using Traits in PHP allows for code reuse in a flexible way without the need for multiple inheritance. Traits can be used to add methods to classes without creating a hierarchy of parent classes, making class construction more modular and maintainable. This approach helps to avoid the issues associated with diamond inheritance and allows for better organization of code.

<?php

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

class User {
    use Logger;
    
    public function createUser() {
        $this->log("User created");
    }
}

$user = new User();
$user->createUser();

?>