0
0
PHPprogramming~20 mins

Multiple interface implementation in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Multiple interface implementation
📖 Scenario: Imagine you are building a simple system where different devices can perform multiple actions. For example, a device might be able to print and scan documents. To organize this, you use interfaces to define these actions.
🎯 Goal: You will create two interfaces, Printer and Scanner, each with one method. Then, you will create a class MultiFunctionDevice that implements both interfaces. Finally, you will create an object of this class and call both methods.
📋 What You'll Learn
Create an interface called Printer with a method printDocument().
Create an interface called Scanner with a method scanDocument().
Create a class called MultiFunctionDevice that implements both Printer and Scanner interfaces.
Implement the methods printDocument() and scanDocument() in the MultiFunctionDevice class.
Create an object of MultiFunctionDevice and call both printDocument() and scanDocument() methods.
Print the outputs exactly as specified.
💡 Why This Matters
🌍 Real World
Many devices like printers, scanners, and fax machines support multiple functions. Using interfaces helps organize these capabilities clearly in code.
💼 Career
Understanding multiple interface implementation is important for designing flexible and maintainable code in object-oriented programming, a common requirement in software development jobs.
Progress0 / 4 steps
1
Create the Printer interface
Create an interface called Printer with a method declaration printDocument().
PHP
Need a hint?

Use the interface keyword to create an interface. Inside, declare the method printDocument() without a body.

2
Create the Scanner interface
Create an interface called Scanner with a method declaration scanDocument().
PHP
Need a hint?

Similar to the first step, create another interface named Scanner with the method scanDocument().

3
Create the MultiFunctionDevice class implementing both interfaces
Create a class called MultiFunctionDevice that implements both Printer and Scanner interfaces. Implement the methods printDocument() and scanDocument() so that printDocument() prints exactly "Printing document..." and scanDocument() prints exactly "Scanning document...".
PHP
Need a hint?

Use the implements keyword to implement multiple interfaces separated by commas. Define both methods with the exact echo statements.

4
Create object and call methods
Create an object called device of the class MultiFunctionDevice. Call the methods printDocument() and scanDocument() on this object, each on a separate line.
PHP
Need a hint?

Create the object with new MultiFunctionDevice(). Call the methods with ->. Use echo "\n"; to print a new line between outputs.