Hint: Syntax: TraitName::method insteadof OtherTrait; inside use block [OK]
Common Mistakes:
Placing insteadof outside the use block
Using as instead of insteadof for conflict resolution
Incorrect order of traits or method names
3. Given the code below, what will be the output?
trait A {
public function hello() {
return "Hello from A";
}
}
trait B {
public function hello() {
return "Hello from B";
}
}
class MyClass {
use A, B {
B::hello insteadof A;
A::hello as helloFromA;
}
}
$obj = new MyClass();
echo $obj->hello();
echo " - ";
echo $obj->helloFromA();
medium
A. Hello from B - Hello from A
B. Hello from A - Hello from B
C. Hello from B - Hello from B
D. Hello from A - Hello from A
Solution
Step 1: Understand insteadof usage
The code uses B::hello insteadof A, so hello() calls use trait B's method.
Step 2: Understand as usage
The code renames A::hello to helloFromA, so $obj->helloFromA() calls trait A's method.
Final Answer:
Hello from B - Hello from A -> Option A
Quick Check:
insteadof picks B, as renames A [OK]
Hint: insteadof picks method; as renames to keep both [OK]
Common Mistakes:
Assuming hello() calls trait A's method
Confusing as with insteadof
Ignoring the renamed method call
4. Identify the error in the following PHP code that tries to resolve trait method conflicts:
trait X {
public function test() {
return "X";
}
}
trait Y {
public function test() {
return "Y";
}
}
class Demo {
use X, Y {
test insteadof X, Y;
}
}
medium
A. Traits cannot have methods with the same name
B. The insteadof syntax is incorrect; method must be prefixed by trait name
C. The class must implement the method explicitly
D. The use statement is missing a semicolon
Solution
Step 1: Check insteadof syntax
The syntax requires specifying which trait's method to use, e.g., X::test insteadof Y;.
Step 2: Identify the error
The code uses test insteadof X, Y; without trait prefix, which is invalid syntax.
Final Answer:
The insteadof syntax is incorrect; method must be prefixed by trait name -> Option B
Quick Check:
Method name needs trait prefix with insteadof [OK]
Hint: Always prefix method with trait name in insteadof [OK]
Common Mistakes:
Omitting trait name before method in insteadof
Thinking traits can't share method names
Forgetting semicolon after use block
5. You have two traits, Logger and FileLogger, both with a method log(). You want a class to use FileLogger::log() by default but also keep Logger::log() accessible under the name basicLog(). Which code snippet correctly achieves this?
hard
A. use Logger, FileLogger {
FileLogger::log as basicLog;
Logger::log insteadof FileLogger;
}
B. use Logger, FileLogger {
Logger::log insteadof FileLogger;
FileLogger::log as basicLog;
}
C. use Logger, FileLogger {
log insteadof Logger, FileLogger;
Logger::log as basicLog;
}
D. use Logger, FileLogger {
FileLogger::log insteadof Logger;
Logger::log as basicLog;
}
Solution
Step 1: Choose default method with insteadof
To use FileLogger::log() by default, write FileLogger::log insteadof Logger;.
Step 2: Rename other method with as
To keep Logger::log() accessible as basicLog(), write Logger::log as basicLog;.
Final Answer:
use Logger, FileLogger {
FileLogger::log insteadof Logger;
Logger::log as basicLog;
} -> Option D
Quick Check:
Default with insteadof, rename with as [OK]
Hint: Use insteadof for default, as to rename [OK]
Common Mistakes:
Swapping traits in insteadof
Using as without insteadof
Incorrect method renaming syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.