Challenge - 5 Problems
Trait Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of trait method usage
What is the output of this PHP code using a trait?
PHP
<?php trait Hello { public function sayHello() { return "Hello from trait!"; } } class Greeter { use Hello; } $g = new Greeter(); echo $g->sayHello(); ?>
Attempts:
2 left
💡 Hint
Traits allow classes to reuse methods defined inside them.
✗ Incorrect
The class Greeter uses the trait Hello, so it inherits the sayHello method. Calling sayHello() on Greeter instance outputs the string defined in the trait.
❓ Predict Output
intermediate2:00remaining
Trait method conflict resolution
What will be the output of this PHP code with two traits having the same method name?
PHP
<?php trait A { public function message() { return "Message from A"; } } trait B { public function message() { return "Message from B"; } } class Test { use A, B { B::message insteadof A; } } $obj = new Test(); echo $obj->message(); ?>
Attempts:
2 left
💡 Hint
The insteadof keyword resolves method conflicts by choosing one trait's method.
✗ Incorrect
The class Test uses both traits A and B, but the conflict is resolved by using B's message method instead of A's. So the output is 'Message from B'.
🔧 Debug
advanced2:00remaining
Identify the error in trait usage
What error will this PHP code produce?
PHP
<?php trait Logger { public function log($msg) { echo $msg; } } class Service { use Logger public function run() { $this->log("Running service\n"); } } $s = new Service(); $s->run(); ?>
Attempts:
2 left
💡 Hint
Check punctuation after the use statement inside the class.
✗ Incorrect
The use statement inside the class is missing a semicolon at the end. This causes a parse error before the public function run() declaration.
🧠 Conceptual
advanced2:00remaining
Trait properties and visibility
Consider this PHP code using a trait with a property. What will be the output?
PHP
<?php trait Counter { private int $count = 0; public function increment() { $this->count++; } public function getCount() { return $this->count; } } class Clicker { use Counter; } $c = new Clicker(); $c->increment(); $c->increment(); echo $c->getCount(); ?>
Attempts:
2 left
💡 Hint
Traits can have properties with visibility, and the class uses them as if declared inside it.
✗ Incorrect
The trait Counter has a private property count initialized to 0. The increment method increases it. After two increments, getCount returns 2.
🚀 Application
expert3:00remaining
Using multiple traits with method aliasing
Given this PHP code, what is the output?
PHP
<?php trait T1 { public function greet() { return "Hello from T1"; } } trait T2 { public function greet() { return "Hello from T2"; } } class MyClass { use T1, T2 { T1::greet as greetT1; T2::greet insteadof T1; } public function greet() { return $this->greetT1() . " and " . T2::greet(); } } $obj = new MyClass(); echo $obj->greet(); ?>
Attempts:
2 left
💡 Hint
Check how trait methods are called inside the class method.
✗ Incorrect
Inside the greet method, T2::greet() is called statically, but trait methods are not static by default. This causes a fatal error.