Bird
Raised Fist0
PHPprogramming~20 mins

Interface declaration and implementation in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of implemented interface method
What is the output of this PHP code when run?
PHP
<?php
interface Talker {
    public function sayHello();
}

class Person implements Talker {
    public function sayHello() {
        echo "Hello from Person!";
    }
}

$p = new Person();
$p->sayHello();
?>
AHello from Person!
BFatal error: Class Person must implement method sayHello()
CHello from Talker!
DNo output
Attempts:
2 left
💡 Hint
Check how the interface method is implemented in the class.
❓ Predict Output
intermediate
2:00remaining
Output when interface method is missing
What error or output will this PHP code produce?
PHP
<?php
interface Flyer {
    public function fly();
}

class Bird implements Flyer {
    // Missing fly() method
}

$b = new Bird();
$b->fly();
?>
ANo output
BCall to undefined method Bird::fly()
CFatal error: Class Bird contains 1 abstract method and must therefore be declared abstract or implement the remaining methods
DFly method called
Attempts:
2 left
💡 Hint
Check if the class implements all interface methods.
🔧 Debug
advanced
2:00remaining
Identify the error in interface implementation
This PHP code tries to implement an interface but causes an error. What is the cause?
PHP
<?php
interface Runner {
    public function run(int $speed);
}

class Athlete implements Runner {
    public function run($speed) {
        echo "Running at speed $speed";
    }
}

$a = new Athlete();
$a->run(10);
?>
AWarning: Argument 1 passed to run() must be of the type int
BFatal error due to missing type hint in method parameter
CNo error, code runs and outputs 'Running at speed 10'
DParse error: syntax error in interface declaration
Attempts:
2 left
💡 Hint
Check if parameter types must match exactly in interface and implementation.
📝 Syntax
advanced
2:00remaining
Which code causes a syntax error?
Which of these PHP code snippets will cause a syntax error?
A?php interface A { public function foo(); } class B implements A { public function foo() { echo 'Hi'; } } ?>
B<?php interface A { public function foo(); } class B implements A { public function foo() { echo 'Hi'; } } ?>
C>? } } ;'iH' ohce { )(oof noitcnuf cilbup { A stnemelpmi B ssalc } ;)(oof noitcnuf cilbup { A ecafretni php?<
D<?php interface A { public function foo(); } class B implements A { public function foo() { echo 'Hi' } } ?>
Attempts:
2 left
💡 Hint
Look carefully at the semicolons inside the method.
🚀 Application
expert
2:00remaining
Number of methods in implemented interface
Given this PHP code, how many methods must class C implement to satisfy all interfaces?
PHP
<?php
interface X {
    public function a();
}
interface Y {
    public function b();
    public function c();
}
interface Z extends X, Y {
    public function d();
}
class C implements Z {
    public function a() {}
    public function b() {}
    public function c() {}
    public function d() {}
}
?>
A4
B1
C2
D3
Attempts:
2 left
💡 Hint
Remember that interface Z extends X and Y, so it includes all their methods.

Practice

(1/5)
1. What is the main purpose of an interface in PHP?
easy
A. To execute code automatically when a program runs
B. To store variables and their values
C. To define method names without their code, ensuring classes implement them
D. To create objects directly

Solution

  1. Step 1: Understand what an interface is

    An interface in PHP only declares method names without any code inside them.
  2. Step 2: Understand the purpose of interfaces

    Interfaces ensure that any class implementing them must write the declared methods, helping organize code.
  3. Final Answer:

    To define method names without their code, ensuring classes implement them -> Option C
  4. Quick Check:

    Interface purpose = method declaration only [OK]
Hint: Interfaces declare methods only, no code inside [OK]
Common Mistakes:
  • Thinking interfaces store variables
  • Believing interfaces create objects
  • Confusing interfaces with classes
2. Which of the following is the correct way to declare an interface named Vehicle in PHP?
easy
A. class Vehicle { public function start(); }
B. interface Vehicle { public function start(); }
C. interface Vehicle { function start() {} }
D. interface Vehicle { public function start() {} }

Solution

  1. Step 1: Recall interface method declaration rules

    In interfaces, methods are declared without code blocks (no curly braces with code).
  2. Step 2: Check each option

    'interface Vehicle { public function start(); }' declares interface and method without code, which is correct. Declarations with method bodies '{}' are wrong. Using 'class' instead of 'interface' is incorrect.
  3. Final Answer:

    interface Vehicle { public function start(); } -> Option B
  4. Quick Check:

    Interface methods have no body [OK]
Hint: Interface methods end with semicolon, no braces [OK]
Common Mistakes:
  • Adding method bodies inside interfaces
  • Using class keyword instead of interface
  • Omitting semicolon after method declaration
3. What will be the output of this PHP code?
interface Animal {
  public function sound();
}

class Dog implements Animal {
  public function sound() {
    return "Bark";
  }
}

$dog = new Dog();
echo $dog->sound();
medium
A. Bark
B. sound
C. Animal
D. Fatal error

Solution

  1. Step 1: Understand interface and class implementation

    The interface Animal declares method sound(). Dog class implements Animal and defines sound() returning "Bark".
  2. Step 2: Check the output of calling $dog->sound()

    Calling $dog->sound() returns "Bark" which is then echoed.
  3. Final Answer:

    Bark -> Option A
  4. Quick Check:

    Implemented method returns "Bark" [OK]
Hint: Implemented methods run normally, output their return [OK]
Common Mistakes:
  • Expecting interface name as output
  • Confusing method name with output
  • Thinking code causes error without implementation
4. Identify the error in this PHP code:
interface Shape {
  public function area();
}

class Circle implements Shape {
  public function area() {
    return 3.14 * 5 * 5;
  }

  public function perimeter() {
    return 2 * 3.14 * 5;
  }
}

$circle = new Circle();
echo $circle->area();
medium
A. No error, code runs and outputs area
B. Missing implementation of perimeter() in interface
C. Interface method area() has no body, causing error
D. Class Circle must implement all interface methods

Solution

  1. Step 1: Check interface and class methods

    Interface Shape declares area(). Circle implements area() correctly and adds extra method perimeter() which is allowed.
  2. Step 2: Verify if code runs without error

    Since all interface methods are implemented, extra methods are allowed. Code outputs area value.
  3. Final Answer:

    No error, code runs and outputs area -> Option A
  4. Quick Check:

    Extra methods allowed, interface methods implemented [OK]
Hint: Extra methods in class are allowed beyond interface [OK]
Common Mistakes:
  • Thinking extra methods must be in interface
  • Confusing method body rules in interface
  • Assuming missing interface methods cause error here
5. You have two interfaces:
interface Printer {
  public function printDocument();
}

interface Scanner {
  public function scanDocument();
}

How can you create a class AllInOne that implements both interfaces and provides method bodies?
hard
A. class AllInOne implements Scanner { public function printDocument() { return "Printing..."; } public function scanDocument() { return "Scanning..."; } }
B. class AllInOne implements Printer { public function printDocument() { return "Printing..."; } public function scanDocument() { return "Scanning..."; } }
C. class AllInOne { public function printDocument() { return "Printing..."; } public function scanDocument() { return "Scanning..."; } }
D. class AllInOne implements Printer, Scanner { public function printDocument() { return "Printing..."; } public function scanDocument() { return "Scanning..."; } }

Solution

  1. Step 1: Understand multiple interface implementation

    PHP allows a class to implement multiple interfaces by listing them separated by commas.
  2. Step 2: Check which option correctly implements both interfaces

    class AllInOne implements Printer, Scanner { public function printDocument() { return "Printing..."; } public function scanDocument() { return "Scanning..."; } } implements both Printer and Scanner and defines both required methods. Others miss one interface or do not implement any.
  3. Final Answer:

    class AllInOne implements Printer, Scanner { public function printDocument() { return "Printing..."; } public function scanDocument() { return "Scanning..."; } } -> Option D
  4. Quick Check:

    Multiple interfaces implemented with commas [OK]
Hint: Use commas to implement multiple interfaces in one class [OK]
Common Mistakes:
  • Implementing only one interface when multiple required
  • Forgetting to implement all interface methods
  • Not using commas to separate interfaces
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