Challenge - 5 Problems
PHP Traits Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of using two traits with same method name
What will be the output of this PHP code using two traits with the same method name and conflict resolution?
PHP
<?php trait A { public function greet() { return "Hello from A"; } } trait B { public function greet() { return "Hello from B"; } } class MyClass { use A, B { B::greet insteadof A; A::greet as greetFromA; } } $obj = new MyClass(); echo $obj->greet(); echo "\n"; echo $obj->greetFromA();
Attempts:
2 left
💡 Hint
Look at how the conflict between greet methods is resolved using insteadof and as.
✗ Incorrect
The class uses both traits A and B. The conflict in greet() is resolved by using B's greet() instead of A's. The A's greet() is still accessible via alias greetFromA(). So calling greet() outputs B's version, and greetFromA() outputs A's version.
❓ Predict Output
intermediate2:00remaining
Output when traits use private properties with same name
What will be the output of this PHP code where two traits have private properties with the same name?
PHP
<?php trait X { private $value = 10; public function getValue() { return $this->value; } } trait Y { private $value = 20; public function getValueY() { return $this->value; } } class Test { use X, Y; } $obj = new Test(); echo $obj->getValue(); echo "\n"; echo $obj->getValueY();
Attempts:
2 left
💡 Hint
Remember that private properties in traits are separate for each trait.
✗ Incorrect
Each trait has its own private property $value. They do not conflict because private properties are scoped to the trait. So getValue() returns 10 from trait X, and getValueY() returns 20 from trait Y.
🔧 Debug
advanced2:00remaining
Identify error in trait method conflict resolution
What error will this PHP code produce and why?
PHP
<?php trait T1 { public function foo() { return "T1"; } } trait T2 { public function foo() { return "T2"; } } class C { use T1, T2 { T1::foo insteadof T2; T2::foo; } } $c = new C(); echo $c->foo();
Attempts:
2 left
💡 Hint
Check the syntax of the trait use conflict resolution block.
✗ Incorrect
The line 'T2::foo;' is invalid syntax. To resolve conflicts, you must use insteadof or as. Just writing 'T2::foo;' alone causes a syntax error.
❓ Predict Output
advanced2:00remaining
Output of trait method overriding in class
What will be the output of this PHP code where a class overrides a method from a trait?
PHP
<?php trait Logger { public function log() { return "Logging from trait"; } } class App { use Logger; public function log() { return "Logging from class"; } } $app = new App(); echo $app->log();
Attempts:
2 left
💡 Hint
Class methods override trait methods if they have the same name.
✗ Incorrect
When a class defines a method with the same name as a trait method, the class method overrides the trait method. So calling log() outputs the class version.
🧠 Conceptual
expert2:00remaining
Number of methods in class using multiple traits with overlapping methods
Given these traits and class, how many methods does the class have after using both traits with conflict resolution?
PHP
<?php trait Alpha { public function a() {} public function b() {} } trait Beta { public function b() {} public function c() {} } class Gamma { use Alpha, Beta { Beta::b insteadof Alpha; Alpha::b as bAlpha; } } $methods = get_class_methods('Gamma'); echo count($methods);
Attempts:
2 left
💡 Hint
Count all unique methods including aliases after conflict resolution.
✗ Incorrect
Gamma class has methods: a (from Alpha), b (from Beta, due to insteadof), c (from Beta), and bAlpha (alias of Alpha's b). So total 4 methods.