What is the difference between traits and traditional inheritance in PHP?

Traits in PHP allow you to reuse methods in multiple classes without using inheritance. This is useful when you want to share methods among different classes that are not necessarily related through a common parent class. In contrast, traditional inheritance involves creating a hierarchy of classes where child classes inherit properties and methods from a parent class.

<?php
// Using traits to share methods among classes
trait Greeting {
    public function sayHello() {
        echo "Hello!";
    }
}

class Person {
    use Greeting;
}

class Animal {
    use Greeting;
}

$person = new Person();
$person->sayHello(); // Output: Hello!

$animal = new Animal();
$animal->sayHello(); // Output: Hello!
?>