0
0
PHPprogramming~20 mins

Multiple trait usage in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Traits Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
A
Hello from B
Hello from A
B
Hello from A
Hello from B
C
Hello from A
Hello from A
DFatal error: Trait method greet has not been applied, because there are collisions
Attempts:
2 left
💡 Hint
Look at how the conflict between greet methods is resolved using insteadof and as.
Predict Output
intermediate
2: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();
AFatal error: Cannot redeclare property $value
B
20
20
C
10
10
D
10
20
Attempts:
2 left
💡 Hint
Remember that private properties in traits are separate for each trait.
🔧 Debug
advanced
2: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();
ASyntax error: Unexpected token ';' in trait use
BT1
CT2
DFatal error: Trait method foo has not been applied, because there are collisions
Attempts:
2 left
💡 Hint
Check the syntax of the trait use conflict resolution block.
Predict Output
advanced
2: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();
Anull
BLogging from class
CFatal error: Cannot override trait method
DLogging from trait
Attempts:
2 left
💡 Hint
Class methods override trait methods if they have the same name.
🧠 Conceptual
expert
2: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);
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Count all unique methods including aliases after conflict resolution.