Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to use the trait method in the class.
PHP
<?php
trait Hello {
public function sayHello() {
return "Hello from Trait!";
}
}
class Greeting {
use Hello;
public function greet() {
return $this->[1]();
}
}
$greet = new Greeting();
echo $greet->greet();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name not defined in the trait.
Trying to call a method that doesn't exist.
✗ Incorrect
The method defined in the trait is 'sayHello', so calling $this->sayHello() uses the trait method.
2fill in blank
mediumComplete the code to resolve the method conflict between two traits.
PHP
<?php
trait A {
public function message() {
return "Message from Trait A";
}
}
trait B {
public function message() {
return "Message from Trait B";
}
}
class Talker {
use A, B {
B::message [1] A;
}
}
$talk = new Talker();
echo $talk->message();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insteadOf' with capital letters.
Using underscores or spaces in the keyword.
✗ Incorrect
The keyword 'insteadof' is used in PHP traits to resolve conflicts by specifying which trait's method to use.
3fill in blank
hardFix the error in the code to alias a trait method correctly.
PHP
<?php
trait Logger {
public function log() {
return "Logging message";
}
}
class Application {
use Logger {
log as [1];
}
public function run() {
return $this->writeLog();
}
}
$app = new Application();
echo $app->run();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same method name as alias.
Using invalid method names with underscores or spaces.
✗ Incorrect
The alias name must be a valid method name different from the original. 'writeLog' is a proper alias.
4fill in blank
hardFill both blanks to correctly resolve conflict and alias a trait method.
PHP
<?php
trait X {
public function action() {
return "Action from X";
}
}
trait Y {
public function action() {
return "Action from Y";
}
}
class Performer {
use X, Y {
Y::action [1] X;
X::action as [2];
}
}
$p = new Performer();
echo $p->action();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keyword for conflict resolution.
Using invalid alias names.
✗ Incorrect
Use 'insteadof' to choose Y's action over X's, and alias X's action as 'performAction'.
5fill in blank
hardFill all three blanks to alias methods and resolve trait conflicts properly.
PHP
<?php
trait Alpha {
public function process() {
return "Processing in Alpha";
}
}
trait Beta {
public function process() {
return "Processing in Beta";
}
}
class Handler {
use Alpha, Beta {
Beta::process [1] Alpha;
Alpha::process as [2];
Beta::process as [3];
}
}
$h = new Handler();
echo $h->process();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keyword spelling.
Using invalid alias names or duplicates.
✗ Incorrect
Use 'insteadof' to prefer Beta's process, alias Alpha's process as 'alphaProcess', and Beta's as 'betaProcess'.