Is it recommended to use traits instead of extends for code reusability in PHP, especially in the context of logging and error handling?

Using traits instead of extends for code reusability in PHP can be a good practice, especially in the context of logging and error handling. Traits allow you to share methods among different classes without creating a hierarchical relationship, which can be beneficial when dealing with multiple classes that need to have similar functionality but do not necessarily share a common parent class.

<?php

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

class MyClass {
    use Logger;

    public function doSomething() {
        $this->log("Doing something...");
    }
}

class AnotherClass {
    use Logger;

    public function doSomethingElse() {
        $this->log("Doing something else...");
    }
}

$myObject = new MyClass();
$myObject->doSomething();

$anotherObject = new AnotherClass();
$anotherObject->doSomethingElse();

?>