How can the use of Traits in PHP affect the overall design and structure of a program?
Using Traits in PHP can affect the overall design and structure of a program by allowing for code reuse in classes that do not have a parent-child relationship. Traits can help avoid code duplication and make the codebase more modular and maintainable. However, overusing traits can lead to code that is harder to understand and maintain, so it's important to use them judiciously.
trait Logger {
public function log($message) {
echo $message;
}
}
class User {
use Logger;
public function greet() {
$this->log('Hello, world!');
}
}
$user = new User();
$user->greet();