0
0
PHPprogramming~10 mins

Multiple trait usage in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use two traits in the class.

PHP
<?php
trait TraitOne {
    public function methodOne() {
        return "One";
    }
}
trait TraitTwo {
    public function methodTwo() {
        return "Two";
    }
}
class MyClass {
    use [1];
}
$object = new MyClass();
echo $object->methodOne() . $object->methodTwo();
Drag options to blanks, or click blank then click option'
ATraitTwo
BTraitOne
CTraitOne, TraitTwo
DTraitThree
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one trait name when two are needed.
Forgetting to separate trait names with commas.
2fill in blank
medium

Complete the code to resolve method name conflict using 'insteadof'.

PHP
<?php
trait TraitA {
    public function hello() {
        return "Hello from A";
    }
}
trait TraitB {
    public function hello() {
        return "Hello from B";
    }
}
class MyClass {
    use TraitA, TraitB {
        TraitB::hello [1] TraitA;
    }
}
$object = new MyClass();
echo $object->hello();
Drag options to blanks, or click blank then click option'
Ainsteadof
Binstead
Cinstead_of
DinsteadOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect spelling like 'instead' or 'insteadOf'.
Not resolving the conflict causing an error.
3fill in blank
hard

Fix the error by completing the code to alias a trait method.

PHP
<?php
trait TraitX {
    public function greet() {
        return "Hi from TraitX";
    }
}
trait TraitY {
    public function greet() {
        return "Hi from TraitY";
    }
}
class MyClass {
    use TraitX, TraitY {
        TraitX::greet as [1];
        TraitY::greet insteadof TraitX;
    }
}
$object = new MyClass();
echo $object->greet();
echo $object->greetAlias();
Drag options to blanks, or click blank then click option'
AgreetAliasMethod
BgreetAlias
CaliasGreet
DgreetY
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid method names for alias.
Not aliasing the method causing conflicts.
4fill in blank
hard

Fill both blanks to correctly use multiple traits and resolve method conflict.

PHP
<?php
trait Alpha {
    public function action() {
        return "Alpha action";
    }
}
trait Beta {
    public function action() {
        return "Beta action";
    }
}
class Performer {
    use [1], [2] {
        Beta::action insteadof Alpha;
    }
}
$performer = new Performer();
echo $performer->action();
Drag options to blanks, or click blank then click option'
AAlpha
BBeta
CGamma
DDelta
Attempts:
3 left
💡 Hint
Common Mistakes
Using traits not defined in the code.
Not separating trait names with commas.
5fill in blank
hard

Fill all three blanks to alias and use multiple traits with conflict resolution.

PHP
<?php
trait First {
    public function speak() {
        return "First speaks";
    }
}
trait Second {
    public function speak() {
        return "Second speaks";
    }
}
class Speaker {
    use [1], [2] {
        Second::speak insteadof First;
        First::speak as [3];
    }
}
$speaker = new Speaker();
echo $speaker->speak();
echo $speaker->firstSpeak();
Drag options to blanks, or click blank then click option'
AFirst
BSecond
CfirstSpeak
DspeakAlias
Attempts:
3 left
💡 Hint
Common Mistakes
Not aliasing the conflicting method.
Using incorrect trait names or alias names.