Complete the code to use two traits in the class.
<?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();Complete the code to resolve method name conflict using 'insteadof'.
<?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();Fix the error by completing the code to alias a trait method.
<?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();Fill both blanks to correctly use multiple traits and resolve method conflict.
<?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();Fill all three blanks to alias and use multiple traits with conflict resolution.
<?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();