Bird
Raised Fist0
PHPprogramming~10 mins

Multiple interface implementation in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class that implements two interfaces.

PHP
<?php
interface A {}
interface B {}
class MyClass implements [1] {}
?>
Drag options to blanks, or click blank then click option'
AA | B
BA & B
CA, B
DA B
Attempts:
3 left
💡 Hint
Common Mistakes
Using & or | instead of commas
Omitting commas between interfaces
2fill in blank
medium

Complete the code to define interface B with a method named 'bar'.

PHP
<?php
interface B {
    public function [1]();
}
?>
Drag options to blanks, or click blank then click option'
Abar
Bfoo
Cbaz
Dqux
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names
Omitting parentheses
3fill in blank
hard

Fix the error in the class method implementation for interface A's method 'foo'.

PHP
<?php
interface A {
    public function foo();
}
class MyClass implements A {
    public function [1]() {
        echo "Hello";
    }
}
?>
Drag options to blanks, or click blank then click option'
Abaz
Bqux
Cbar
Dfoo
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names
Missing method implementation
4fill in blank
hard

Fill both blanks to complete the class implementing interfaces A and B with required methods.

PHP
<?php
interface A {
    public function foo();
}
interface B {
    public function bar();
}
class MyClass implements A, B {
    public function [1]() {
        echo "foo method";
    }
    public function [2]() {
        echo "bar method";
    }
}
?>
Drag options to blanks, or click blank then click option'
Afoo
Bbar
Cbaz
Dqux
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping method names
Omitting one method
5fill in blank
hard

Fill all three blanks to create a class that implements interfaces A and B and calls both methods.

PHP
<?php
interface A {
    public function foo();
}
interface B {
    public function bar();
}
class MyClass implements A, B {
    public function [1]() {
        echo "foo called";
    }
    public function [2]() {
        echo "bar called";
    }
}
$obj = new MyClass();
$obj->[3]();
$obj->bar();
?>
Drag options to blanks, or click blank then click option'
Afoo
Bbar
Cbaz
Dqux
Attempts:
3 left
💡 Hint
Common Mistakes
Calling undefined methods
Mismatching method names

Practice

(1/5)
1. What does it mean when a PHP class implements multiple interfaces separated by commas?
easy
A. The class can only use one interface at a time.
B. The class inherits properties from multiple parent classes.
C. The class automatically creates methods from the interfaces.
D. The class promises to have all methods defined in all those interfaces.

Solution

  1. Step 1: Understand interface implementation

    When a class implements interfaces, it promises to include all methods declared in those interfaces.
  2. Step 2: Multiple interfaces separated by commas

    Listing interfaces separated by commas means the class must implement all methods from each interface.
  3. Final Answer:

    The class promises to have all methods defined in all those interfaces. -> Option D
  4. Quick Check:

    Multiple interfaces = all methods implemented [OK]
Hint: Remember: commas mean all interfaces must be implemented [OK]
Common Mistakes:
  • Thinking interfaces provide properties
  • Confusing interfaces with class inheritance
  • Believing methods are auto-created
2. Which of the following is the correct syntax for a PHP class implementing two interfaces named FirstInterface and SecondInterface?
easy
A. class MyClass extends FirstInterface, SecondInterface {}
B. class MyClass implements FirstInterface, SecondInterface {}
C. class MyClass implements FirstInterface SecondInterface {}
D. class MyClass uses FirstInterface, SecondInterface {}

Solution

  1. Step 1: Identify correct keyword for interfaces

    PHP uses the keyword implements to indicate a class uses interfaces.
  2. Step 2: Check syntax for multiple interfaces

    Multiple interfaces are listed separated by commas after implements.
  3. Final Answer:

    class MyClass implements FirstInterface, SecondInterface {} -> Option B
  4. Quick Check:

    implements + comma-separated interfaces = correct syntax [OK]
Hint: Use 'implements' and commas to list interfaces [OK]
Common Mistakes:
  • Using 'extends' instead of 'implements' for interfaces
  • Missing commas between interface names
  • Using 'uses' keyword incorrectly
3. 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();
medium
A. foo bar
B. foobar
C. Syntax error
D. Runtime error

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]
Hint: 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
4. Identify the error in this PHP code:
interface X { public function methodX(); }
interface Y { public function methodY(); }
class Demo implements X, Y {
public function methodX() { return 'X'; }
}
medium
A. Interface Y cannot be implemented with interface X.
B. Class Demo should extend interfaces, not implement.
C. Class Demo must implement methodY() from interface Y.
D. No error, code is correct.

Solution

  1. Step 1: Check implemented interfaces and methods

    Class Demo implements interfaces X and Y, so it must define all methods from both.
  2. Step 2: Identify missing method

    methodY() from interface Y is not implemented, causing an error.
  3. Final Answer:

    Class Demo must implement methodY() from interface Y. -> Option C
  4. Quick Check:

    All interface methods must be implemented [OK]
Hint: Implement all methods from every interface you list [OK]
Common Mistakes:
  • Implementing only some interface methods
  • Confusing extends with implements
  • Assuming interfaces can be partially implemented
5. Given these interfaces and classes:
interface Logger { public function log(string $msg); }
interface Notifier { public function notify(string $msg); }
class System implements Logger, Notifier {
private array $logs = [];
public function log(string $msg) { $this->logs[] = $msg; }
public function notify(string $msg) { echo "Notify: $msg\n"; }
public function getLogs(): array { return $this->logs; }
}
$sys = new System();
$sys->log('Start');
$sys->notify('Alert');
$sys->log('End');
print_r($sys->getLogs());

What is the output of this code?
hard
A. Notify: Alert Array ( [0] => Start [1] => End )
B. Array ( [0] => Start [1] => End ) Notify: Alert
C. Notify: Alert Array ( [0] => Alert )
D. Syntax error due to missing interface methods

Solution

  1. Step 1: Trace method calls

    First, log('Start') adds 'Start' to logs array. Then notify('Alert') echoes 'Notify: Alert'. Then log('End') adds 'End' to logs.
  2. Step 2: Understand print_r output

    print_r outputs the logs array with 'Start' and 'End' entries after the echoed notify message.
  3. Final Answer:

    Notify: Alert Array ( [0] => Start [1] => End ) -> Option A
  4. Quick Check:

    Echo notify first, then print logs array [OK]
Hint: Echo outputs appear before print_r array display [OK]
Common Mistakes:
  • Assuming print_r output appears before echo
  • Confusing array contents logged
  • Expecting syntax errors without missing methods
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard