I have been struggling to understand what an interface actually does. Following is a very clear example that should be clear to other beginners.
Could somone confirm (or not) that the interface does nothing? It acts like an elaborate comment? Moreover, if you used the same interface in two classes, you would have to define the functions and this might be un-DRY? Or have I missed something?
<?php
interface fax{
public function dial();
public function send();
public function recieve();
}
interface printer{
public function printBlack();
public function printColor();
public function printDraft();
public function kick();
}
class printerFax implements fax, printer{
public function dial(){ }
public function send(){ }
public function recieve(){ }
public function printBlack(){ }
public function printColor(){ }
public function printDraft(){ }
public function kick(){ }
}
$object = new printerFax;
?>
Further thoughts, to be more specific: what does the interface actually do? I asked if it is a comment - many people imply that it is. I asked if it wasn't DRY.
I have since found this example. What does it mean to "program to an interface"? . I might add it is the only example I have found in hours of searching.
Thanks for your ready responses. If a beginner arrives here, please go to question 383947. The example is not in PHP but it shows the functionality. If anyone knows an example in PHP, I would be grateful.
This still seems undry though. Functions get repeated where they wouldn't in procedural programming.