Complete the code to declare a trait named Logger.
<?php
trait [1] {
public function log($msg) {
echo $msg;
}
}
?>The trait is declared with the keyword trait followed by its name. Here, the correct name is Logger.
Complete the code to use the trait Logger inside the class Application.
<?php class Application { use [1]; } ?>
The use keyword inside a class includes the trait. The trait name is Logger.
Fix the error in the code by completing the trait usage syntax.
<?php
trait Logger {
public function log($msg) {
echo $msg;
}
}
class App {
[1] Logger;
}
?>The correct keyword to include a trait inside a class is use.
Fill both blanks to declare a trait and use it inside a class.
<?php [1] Logger { public function log($msg) { echo $msg; } } class System { [2] Logger; } ?>
The trait is declared with trait and included in the class with use.
Fill all three blanks to create a trait, use it in a class, and call its method.
<?php [1] Logger { public function log($msg) { echo $msg; } } class App { [2] Logger; public function run() { $this->[3]("Hello from trait!\n"); } } $app = new App(); $app->run(); ?>
The trait is declared with trait, included in the class with use, and the method log is called.
