Bird
Raised Fist0
PHPprogramming~10 mins

Interface declaration and implementation in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Interface declaration and implementation
Declare Interface
↓
Define Methods (no body)
↓
Create Class
↓
Implement Interface Methods
↓
Instantiate Class
↓
Call Implemented Methods
This flow shows how an interface is declared with method signatures, then a class implements those methods, and finally the class is used.
Execution Sample
PHP
<?php
interface Animal {
  public function makeSound();
}

class Dog implements Animal {
  public function makeSound() {
    echo "Woof!";
  }
}

$dog = new Dog();
$dog->makeSound();
?>
This code declares an interface Animal with a method makeSound, then a Dog class implements it and outputs 'Woof!'.
Execution Table
StepActionEvaluationResult
1Declare interface AnimalDefines method makeSound()Interface Animal ready
2Define class Dog implementing AnimalDog must implement makeSound()Class Dog ready
3Implement makeSound() in DogMethod outputs 'Woof!'Method implemented
4Instantiate Dog object$dog = new Dog()Object $dog created
5Call $dog->makeSound()Executes methodOutputs: Woof!
6End of scriptNo more codeExecution stops
💡 Script ends after method call outputs 'Woof!'
Variable Tracker
VariableStartAfter Step 4After Step 5Final
$dogundefinedObject of class DogObject of class DogObject of class Dog
Key Moments - 3 Insights
Why does the Dog class have to implement makeSound()?
Because the Dog class implements the Animal interface, it must provide the method makeSound() as declared in the interface (see execution_table step 2 and 3).
What happens if Dog does not implement makeSound()?
PHP will give an error because the class does not fulfill the interface contract (refer to execution_table step 2 where implementation is required).
Does the interface contain method bodies?
No, interfaces only declare method signatures without bodies (see execution_table step 1). The class provides the actual method code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when $dog->makeSound() is called at step 5?
AError
BWoof!
CMeow!
DNo output
💡 Hint
Check execution_table row 5 for the output of makeSound()
At which step is the Dog object created?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at execution_table row 4 for object instantiation
If the Dog class did not implement makeSound(), what would happen at step 3?
AThe script runs normally
BmakeSound() outputs nothing
CPHP throws an error
DDog class is abstract
💡 Hint
Refer to key_moments about missing method implementation
Concept Snapshot
interface InterfaceName {
  public function methodName();
}

class ClassName implements InterfaceName {
  public function methodName() {
    // method body
  }
}

- Interfaces declare method signatures only.
- Classes implementing interfaces must define all methods.
- Helps enforce consistent method names across classes.
Full Transcript
This example shows how to declare an interface in PHP with a method signature. The Dog class implements this interface and provides the method body. When we create a Dog object and call makeSound(), it outputs 'Woof!'. The interface ensures that any class implementing it must have the declared methods. If a class does not implement all interface methods, PHP will throw an error. Interfaces do not contain method bodies themselves, only declarations.

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