0
0
PHPprogramming~10 mins

Trait conflict resolution 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 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'
Ahello
BsayHello
Cgreet
DsayHi
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name not defined in the trait.
Trying to call a method that doesn't exist.
2fill in blank
medium

Complete 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'
Ainsteadof
Binstead_of
CinsteadOf
Dinstead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insteadOf' with capital letters.
Using underscores or spaces in the keyword.
3fill in blank
hard

Fix 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'
Alog
BlogMessage
CwriteLog
Dlog_as
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same method name as alias.
Using invalid method names with underscores or spaces.
4fill in blank
hard

Fill 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'
Ainsteadof
BinsteadOf
CperformAction
DdoAction
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keyword for conflict resolution.
Using invalid alias names.
5fill in blank
hard

Fill 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'
Ainsteadof
BalphaProcess
CbetaProcess
DinsteadOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keyword spelling.
Using invalid alias names or duplicates.