0
0
PHPprogramming~20 mins

Trait conflict resolution in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trait Conflict Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of trait method conflict resolution with 'insteadof'
What is the output of this PHP code that uses two traits with conflicting method names resolved by 'insteadof'?
PHP
<?php
trait A {
    public function sayHello() {
        return "Hello from A";
    }
}
trait B {
    public function sayHello() {
        return "Hello from B";
    }
}
class Greeting {
    use A, B {
        B::sayHello insteadof A;
    }
}
$greet = new Greeting();
echo $greet->sayHello();
AHello from A
BHello from B
CFatal error: Trait method sayHello has not been applied, because there are collisions
DHello from A and B
Attempts:
2 left
💡 Hint
Look at which trait's method is chosen by the 'insteadof' operator.
Predict Output
intermediate
2:00remaining
Output when aliasing trait methods with 'as'
What will this PHP code output when aliasing a trait method with 'as' to create a new method name?
PHP
<?php
trait X {
    public function greet() {
        return "Hello from X";
    }
}
class Y {
    use X {
        greet as sayHi;
    }
}
$y = new Y();
echo $y->sayHi();
AHello from X
BHello from Y
CFatal error: Call to undefined method Y::sayHi()
DHello from X and Y
Attempts:
2 left
💡 Hint
Aliasing creates a new method name pointing to the original trait method.
🔧 Debug
advanced
2:00remaining
Identify the error in trait conflict resolution
This PHP code tries to resolve a method conflict between two traits but causes an error. What is the error message?
PHP
<?php
trait T1 {
    public function action() {
        return "Action from T1";
    }
}
trait T2 {
    public function action() {
        return "Action from T2";
    }
}
class C {
    use T1, T2 {
        action;
    }
}
$c = new C();
echo $c->action();
AFatal error: Trait method action has not been applied, because there are collisions
BAction from T1
CAction from T2
DParse error: syntax error, unexpected ';' in class C
Attempts:
2 left
💡 Hint
Check the syntax of the conflict resolution block inside the use statement.
Predict Output
advanced
2:00remaining
Output when using both 'insteadof' and 'as' in trait conflict resolution
What is the output of this PHP code that uses 'insteadof' to resolve conflict and 'as' to alias a method?
PHP
<?php
trait Alpha {
    public function speak() {
        return "Alpha speaking";
    }
}
trait Beta {
    public function speak() {
        return "Beta speaking";
    }
}
class Speaker {
    use Alpha, Beta {
        Alpha::speak insteadof Beta;
        Beta::speak as speakBeta;
    }
}
$s = new Speaker();
echo $s->speak() . ' and ' . $s->speakBeta();
AAlpha speaking and Alpha speaking
BBeta speaking and Alpha speaking
CAlpha speaking and Beta speaking
DFatal error: Call to undefined method Speaker::speakBeta()
Attempts:
2 left
💡 Hint
The 'insteadof' chooses which speak() method is default, 'as' creates an alias for the other.
🧠 Conceptual
expert
2:00remaining
Understanding trait conflict resolution priority
Given two traits with the same method name used in a class, which statement about PHP trait conflict resolution is true?
APHP throws a fatal error if two traits have the same method name and no conflict resolution is provided.
BIf no conflict resolution is specified, PHP uses the method from the trait listed first in the use statement.
CPHP automatically merges the two methods into one combined method.
DThe class's own method always overrides trait methods, even if not declared explicitly.
Attempts:
2 left
💡 Hint
Think about what happens when PHP encounters two methods with the same name from different traits without instructions.