0
0
PHPprogramming~10 mins

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

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