Junior — Middle
Have you applied the concept of traits in PHP for organizing reusable code?
sobes.tech AI
Answer from AI
In PHP, traits are used for code reuse between classes without the need for inheritance. This helps avoid duplication and allows flexible inclusion of common methods in different classes.
Example of using a trait:
trait Logger {
public function log(string $message) {
echo "Log: $message";
}
}
class User {
use Logger;
public function create() {
// user creation logic
$this->log('User created');
}
}
$user = new User();
$user->create();
Thus, traits help organize reusable functionality without a strict class hierarchy.