Challenge 3 — Solution Task: Create a trait Greetable with a method sayHello() that echoes "Hello from " followed by a $name property. Use this trait in two unrelated classes, Robot and Alien (neither extending the other, or any common parent), each with their own $name set via a constructor. Call sayHello() on one instance of each. name}
"; } } class Robot { use Greetable; public function __construct(private string $name) {} } class Alien { use Greetable; public function __construct(private string $name) {} } $robot = new Robot("Unit-7"); $alien = new Alien("Zorblax"); $robot->sayHello(); $alien->sayHello(); ?> Output: Hello from Unit-7 Hello from Zorblax Notes: - Robot and Alien share no inheritance relationship whatsoever - one does not extend the other, and neither extends any common parent class - yet both gain the identical sayHello() method purely by writing "use Greetable;" inside their own class body. - The trait's own sayHello() method references $this->name, which works correctly for both classes because each one independently declares its own $name property via its own constructor - the trait itself declares no properties, it only supplies the method code. - This is exactly the scenario extends cannot solve cleanly: sharing actual, identical method code across classes that have no genuine "is-a" relationship to each other.