0
0
PHPprogramming~10 mins

Multiple interface implementation in PHP - Interactive Code Practice

Choose your learning style9 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