Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one trait name when two are needed.
Forgetting to separate trait names with commas.
✗ Incorrect
To use multiple traits in a class, list them separated by commas after the 'use' keyword.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect spelling like 'instead' or 'insteadOf'.
Not resolving the conflict causing an error.
✗ Incorrect
The 'insteadof' keyword is used to specify which trait's method to use when there is a conflict.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid method names for alias.
Not aliasing the method causing conflicts.
✗ Incorrect
You can create an alias for a trait method by using 'as' followed by the new method name.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using traits not defined in the code.
Not separating trait names with commas.
✗ Incorrect
To use multiple traits, list them separated by commas. Then resolve conflicts with 'insteadof'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not aliasing the conflicting method.
Using incorrect trait names or alias names.
✗ Incorrect
Use both traits with 'use', resolve conflict with 'insteadof', and alias the other method with 'as'.