title: PHP Duck Sample - Head First - Design Patterns - Chapter One
body: <p>This is the first question I ask from many others to come. </p>
  
  <p>Someone here might call me crazy because I'm following the mentioned book in the question's Title using PHP-OO. </p>
  
  <p>At the first chapter, the authors introduce a simple project called 'SimUDuck' and, although I've reproduced the same in Java, I was wondering to reproduce the same using PHP. </p>
  
  <p>At the end, the SimUDuck's project creates two (2) interfaces (FlyBehavior and QuackBehavior), more than five (5) classes implementing those interfaces (e.g. FlyWithWings(), Quack() etc), an abstract class called Duck and three (3) or four (4) different ducks species classes extending Duck (Mallard, HeadRedDuck, RubberDuck etc), just to demonstrate how important is to program for interface. </p>
  
  <p>To simulate the Java main method environment, I've created a PHP class called MiniDuckSimulator, including the function 'public static function main()' and in the same script I've added "MiniDuckSimulator::main();". The script works without errors. </p>
  
  <p>The intriguing issue is that without call any QuackBehavior::quack() implemented method, the echo 'Quack!quack!' appears. Those who has read this book knows what I'm talking about. </p>
  
  <p>Note: Below is a particular part of the script:</p>
  
  <hr>
  
  <pre><code>interface FlyBehavior {
      public function fly();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>interface QuackBehavior {
      public function quack();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>include_once 'FlyBehavior.php';
  
  class FlyWithWings implements FlyBehavior {
  
      public function fly() {
          echo 'I'm flying!&lt;br /&gt;';
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>include_once 'QuackBehavior.php';
  
  class Quack implements QuackBehavior {
  
      public function quack() {
          echo 'Quack!&lt;br /&gt;';
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>abstract class Duck {
  
      protected $flyBehavior;
      protected $quackBehavior;
  
      function  __construct() {
      }
  
      public function performFly(){
          $this-&gt;flyBehavior-&gt;fly();
  
      }
  
      public function performQuack(){
          $this-&gt;quackBehavior-&gt;quack();
  
      }
  
      public function setFlyBehavior($flyBehavior){
          $this-&gt;flyBehavior = $flyBehavior;
      }
  
      public function swim(){
          echo "All the ducks float, including the decoy!&lt;br /&gt;";
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>include_once 'Duck.php';
  include_once 'FlyWithWings.php';
  include_once 'Quack.php';
  
  class Mallard extends Duck {
  
      function __construct() {
          $this-&gt;flyBehavior = new FlyWithWings();
          $this-&gt;quackBehavior = new Quack();
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>class MiniDuckSimulator {
      public static function main(){
          $mallard = new Mallard();
          $mallard-&gt;performFly();
      }
  
  }
  
  MiniDuckSimulator::main();
  </code></pre>
  
  <hr>
  
  <p>Thanks in advance.</p>
  
  <p>LucDaher.</p>