Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Interfaces and Traits
What will be the output of this PHP code?
interface A { public function foo(); }
interface B { public function bar(); }
class Test implements A, B {
public function foo() { echo 'foo'; }
public function bar() { echo 'bar'; }
}
$obj = new Test();
$obj->foo(); $obj->bar();
Afoo bar
Bfoo
Cfoobar
Dbarfoo
Step-by-Step Solution
Solution:
  1. Step 1: Understand method calls

    The object calls foo() which echoes 'foo', then bar() which echoes 'bar'.
  2. Step 2: Combine outputs

    Since echo outputs directly without spaces, the combined output is 'foobar'.
  3. Final Answer:

    foobar -> Option C
  4. Quick Check:

    Method calls output concatenated strings 'foobar' [OK]
Quick Trick: Echo outputs concatenate without spaces unless added [OK]
Common Mistakes:
  • Assuming spaces are added automatically
  • Confusing method call order
  • Expecting only one method's output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes