yoesoff

Untitled

Dec 6th, 2018
135
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. interface HewanInterface {
  4.     function suara(): string;
  5. }
  6.  
  7. class Kucing implements HewanInterface {
  8.     function suara(): string {
  9.         return "meong";
  10.     }
  11. }
  12.  
  13. class Anjing implements HewanInterface {
  14.     function suara(): string {
  15.         return "guk guk";
  16.     }
  17. }
  18.  
  19. // Kucing
  20. $memeng = new Kucing;
  21. echo $memeng->suara();
  22.  
  23. echo PHP_EOL;
  24.  
  25. // Anjing
  26. $gogok = new Anjing;
  27. echo $gogok->suara();
  28.  
  29. echo PHP_EOL;
  30.  
  31.  
  32. class HewanApalah {
  33.     private $hewan;
  34.  
  35.     public function setHewan(HewanInterface $hewan) {
  36.         $this->hewan = $hewan;
  37.     }
  38.  
  39.     public function suara() {
  40.         return $this->hewan->suara();
  41.     }
  42. }
  43.  
  44. $apa = new HewanApalah;
  45.  
  46. $apa->setHewan($gogok);
  47. echo $apa->suara();
  48. echo PHP_EOL;
  49.  
  50. $apa->setHewan($memeng);
  51. echo $apa->suara();
  52. echo PHP_EOL;
RAW Paste Data