Avatar ·

How to override a trait method in PHP and call it from the overridden method?

Hello everyone! I'm learning PHP and ran into a problem with traits. Here's the situation: I have a trait `Logger` that contains a method `log($message)`. This method simply outputs a message to the console. I want to use this trait in the `UserService` class, but I need to change the behavior of the `log` method: add a prefix with the class name to the message. However, after overriding the method, I cannot call the original version from the trait. Here's a code example: ```php trait Logger { public function log($message) { echo "LOG: " . $message . "\n"; } } class UserService { use Logger; public function log($message) { // I want to call the parent trait method, but I don't know how // parent::log($message); // Error! echo "[" . __CLASS__ . "] " . ```