Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
<?php trait Logger { public function log(string $message): void { echo "[Logger] $message "; } public function format(string $text): string { return strtoupper($text); } } trait Debugger { public function log(string $message): void { echo "[Debugger] " . date('H:i:s') . " $message "; } public function format(string $text): string { return "DEBUG: $text"; } } class Application { use Logger, Debugger { // Conflict resolution for log() Logger::log insteadof Debugger; // use Logger's log() Debugger::log as debugLog; // also keep Debugger's as debugLog() // Conflict resolution for format() Debugger::format insteadof Logger; // use Debugger's format() Logger::format as loggerFormat; // keep Logger's as loggerFormat() } } $app = new Application(); $app->log('Application started'); // [Logger] Application started $app->debugLog('Checking state...'); // [Debugger] 14:00:00 Checking state... echo $app->format('hello'); // DEBUG: hello echo $app->loggerFormat('hello'); // HELLO // Change visibility with 'as' trait Greet { public function hello(): string { return 'Hello!'; } } class Shy { use Greet { hello as private; // make it private within this class } }
Result
Open