0
0
PHPprogramming~10 mins

Multiple interface implementation in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple interface implementation
Define Interface A
Define Interface B
Create Class implementing A and B
Implement all methods from A and B
Create object of Class
Call methods from both interfaces
The class promises to follow rules from both interfaces by implementing all their methods.
Execution Sample
PHP
<?php
interface A {
  public function foo();
}
interface B {
  public function bar();
}
class MyClass implements A, B {
  public function foo() { return "foo called"; }
  public function bar() { return "bar called"; }
}
$obj = new MyClass();
echo $obj->foo();
echo $obj->bar();
?>
This code shows a class implementing two interfaces and calling their methods.
Execution Table
StepActionEvaluationResult
1Define interface A with method foo()Interface A createdInterface A ready
2Define interface B with method bar()Interface B createdInterface B ready
3Create class MyClass implementing A and BClass MyClass createdClass ready with interfaces A and B
4Implement method foo() in MyClassMethod foo() definedfoo() returns 'foo called'
5Implement method bar() in MyClassMethod bar() definedbar() returns 'bar called'
6Create object obj of MyClassObject createdobj is instance of MyClass
7Call obj->foo()Calls foo()Output: 'foo called'
8Call obj->bar()Calls bar()Output: 'bar called'
9End of scriptNo more codeExecution stops
💡 All interface methods implemented; object methods called; script ends
Variable Tracker
VariableStartAfter Step 6After Step 7After Step 8Final
objundefinedinstance of MyClassinstance of MyClassinstance of MyClassinstance of MyClass
Key Moments - 2 Insights
Why must MyClass implement all methods from both interfaces A and B?
Because PHP requires that a class implementing interfaces must define all their methods, as shown in steps 4 and 5 of the execution_table.
What happens if MyClass misses implementing one method from an interface?
PHP will give an error and stop execution, because the class does not fulfill the interface contract, which is why steps 4 and 5 are important.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after calling obj->foo() at step 7?
ANo output
B"bar called"
C"foo called"
DError
💡 Hint
Check the 'Result' column at step 7 in the execution_table
At which step does the object obj get created?
AStep 6
BStep 4
CStep 7
DStep 3
💡 Hint
Look for 'Create object obj' in the 'Action' column of execution_table
If MyClass did not implement method bar(), what would happen?
AThe script runs normally
BPHP throws an error at class creation
COnly foo() works, bar() is ignored
DThe object cannot be created
💡 Hint
Refer to key_moments about missing method implementation consequences
Concept Snapshot
Multiple interface implementation in PHP:
- Use 'implements' keyword with comma-separated interfaces.
- Class must define all methods from all interfaces.
- Allows one class to follow multiple contracts.
- Example: class MyClass implements A, B { ... }
- Missing methods cause errors.
Full Transcript
This example shows how a PHP class can implement multiple interfaces by listing them after the 'implements' keyword. Each interface defines methods that the class must provide. The class MyClass implements interfaces A and B, so it defines both foo() and bar() methods. An object of MyClass is created and both methods are called, producing outputs 'foo called' and 'bar called'. If any method from the interfaces is missing, PHP will throw an error. This ensures the class follows all interface rules.