Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Interfaces and Traits
What will be the output of the following PHP code?
interface A { public function foo(); }
interface B { public function bar(); }
class Test implements A, B {
public function foo() { return 'foo'; }
public function bar() { return 'bar'; }
}
$obj = new Test();
echo $obj->foo() . ' ' . $obj->bar();
Afoo bar
Bfoobar
CSyntax error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Check interface methods implemented

    The class Test implements both interfaces A and B and defines both methods foo() and bar() correctly.
  2. Step 2: Understand output of method calls

    Calling foo() returns 'foo' and bar() returns 'bar'. Echo concatenates them with a space.
  3. Final Answer:

    foo bar -> Option A
  4. Quick Check:

    Implemented methods return strings combined with space [OK]
Quick Trick: Check all interface methods are implemented to avoid errors [OK]
Common Mistakes:
  • Forgetting to implement all interface methods
  • Missing space in echo output
  • Expecting concatenation without space

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes