Bird
Raised Fist0
PHPprogramming~20 mins

Trait conflict resolution in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of the insteadof keyword when using traits in PHP?
easy
A. To resolve method name conflicts by choosing which trait's method to use
B. To rename a trait's method to a new name
C. To import all methods from a trait without conflicts
D. To prevent a trait from being used in a class

Solution

  1. Step 1: Understand trait conflicts

    When two traits have methods with the same name, PHP needs a way to decide which one to use.
  2. Step 2: Role of insteadof

    The insteadof keyword explicitly tells PHP which trait's method to use, resolving the conflict.
  3. Final Answer:

    To resolve method name conflicts by choosing which trait's method to use -> Option A
  4. Quick Check:

    insteadof = choose method [OK]
Hint: Use insteadof to pick one method when names clash [OK]
Common Mistakes:
  • Confusing insteadof with as
  • Thinking insteadof renames methods
  • Assuming insteadof imports all methods
2. Which of the following is the correct syntax to resolve a trait method conflict using insteadof in PHP?
easy
A. use TraitA, TraitB { TraitB::methodName as methodName; }
B. use TraitA, TraitB { methodName insteadof TraitA, TraitB; }
C. use TraitA, TraitB { TraitA::methodName insteadof TraitB; }
D. use TraitA, TraitB { TraitA::methodName as insteadof TraitB; }

Solution

  1. Step 1: Review trait conflict syntax

    The correct syntax uses the trait name, method name, and insteadof keyword to specify which trait's method to use.
  2. Step 2: Match syntax to options

    use TraitA, TraitB { TraitA::methodName insteadof TraitB; } correctly shows: TraitA::methodName insteadof TraitB; inside the use block.
  3. Final Answer:

    use TraitA, TraitB { TraitA::methodName insteadof TraitB; } -> Option C
  4. Quick Check:

    Correct insteadof syntax = use TraitA, TraitB { TraitA::methodName insteadof TraitB; } [OK]
Hint: Syntax: TraitName::method insteadof OtherTrait; inside use block [OK]
Common Mistakes:
  • Placing insteadof outside the use block
  • Using as instead of insteadof for conflict resolution
  • Incorrect order of traits or method names
3. Given the code below, what will be the output?
trait A {
    public function hello() {
        return "Hello from A";
    }
}
trait B {
    public function hello() {
        return "Hello from B";
    }
}
class MyClass {
    use A, B {
        B::hello insteadof A;
        A::hello as helloFromA;
    }
}
$obj = new MyClass();
echo $obj->hello();
echo " - ";
echo $obj->helloFromA();
medium
A. Hello from B - Hello from A
B. Hello from A - Hello from B
C. Hello from B - Hello from B
D. Hello from A - Hello from A

Solution

  1. Step 1: Understand insteadof usage

    The code uses B::hello insteadof A, so hello() calls use trait B's method.
  2. Step 2: Understand as usage

    The code renames A::hello to helloFromA, so $obj->helloFromA() calls trait A's method.
  3. Final Answer:

    Hello from B - Hello from A -> Option A
  4. Quick Check:

    insteadof picks B, as renames A [OK]
Hint: insteadof picks method; as renames to keep both [OK]
Common Mistakes:
  • Assuming hello() calls trait A's method
  • Confusing as with insteadof
  • Ignoring the renamed method call
4. Identify the error in the following PHP code that tries to resolve trait method conflicts:
trait X {
    public function test() {
        return "X";
    }
}
trait Y {
    public function test() {
        return "Y";
    }
}
class Demo {
    use X, Y {
        test insteadof X, Y;
    }
}
medium
A. Traits cannot have methods with the same name
B. The insteadof syntax is incorrect; method must be prefixed by trait name
C. The class must implement the method explicitly
D. The use statement is missing a semicolon

Solution

  1. Step 1: Check insteadof syntax

    The syntax requires specifying which trait's method to use, e.g., X::test insteadof Y;.
  2. Step 2: Identify the error

    The code uses test insteadof X, Y; without trait prefix, which is invalid syntax.
  3. Final Answer:

    The insteadof syntax is incorrect; method must be prefixed by trait name -> Option B
  4. Quick Check:

    Method name needs trait prefix with insteadof [OK]
Hint: Always prefix method with trait name in insteadof [OK]
Common Mistakes:
  • Omitting trait name before method in insteadof
  • Thinking traits can't share method names
  • Forgetting semicolon after use block
5. You have two traits, Logger and FileLogger, both with a method log(). You want a class to use FileLogger::log() by default but also keep Logger::log() accessible under the name basicLog(). Which code snippet correctly achieves this?
hard
A. use Logger, FileLogger { FileLogger::log as basicLog; Logger::log insteadof FileLogger; }
B. use Logger, FileLogger { Logger::log insteadof FileLogger; FileLogger::log as basicLog; }
C. use Logger, FileLogger { log insteadof Logger, FileLogger; Logger::log as basicLog; }
D. use Logger, FileLogger { FileLogger::log insteadof Logger; Logger::log as basicLog; }

Solution

  1. Step 1: Choose default method with insteadof

    To use FileLogger::log() by default, write FileLogger::log insteadof Logger;.
  2. Step 2: Rename other method with as

    To keep Logger::log() accessible as basicLog(), write Logger::log as basicLog;.
  3. Final Answer:

    use Logger, FileLogger { FileLogger::log insteadof Logger; Logger::log as basicLog; } -> Option D
  4. Quick Check:

    Default with insteadof, rename with as [OK]
Hint: Use insteadof for default, as to rename [OK]
Common Mistakes:
  • Swapping traits in insteadof
  • Using as without insteadof
  • Incorrect method renaming syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard