title: How do I abstract a task when using polymorphism?
body: <p>I've read numerous SO posts about Polymorphism, and also the other really good one at <a href="http://net.tutsplus.com/tutorials/php/understanding-and-applying-polymorphism-in-php/" rel="nofollow">http://net.tutsplus.com/tutorials/php/understanding-and-applying-polymorphism-in-php/</a> Good stuff!</p>
  
  <p>---EDIT---
  Inside of the PDFFormatter class, I had to use (instanceof) in order to figure out if some code should be included in the returned data because I was hard coding the individual field names inside the formatter. How can I abstract that responsibility? <strong><em>I AM ASSUMING THIS IS NOT THE BEST WAY</em></strong></p>
  
  <p>I am also trying to pass in different kinds of profiles to be formatted. Note also that there can be many formatter types. Thanks sooooooo much in advance! Please PHP only! thanks!</p>
  
  <p>File 1. FormatterInterface.php</p>
  
  <pre><code>interface FormatterInterface
  {
      public function format(Profile $Profile);
  }
  </code></pre>
  
  <p>File 2. PDFFormatter.php</p>
  
  <pre><code>class PDFFormatter implements FormatterInterface
  {
      public function format(Profile $Profile)
      {
          $format = "PDF Format&lt;br /&gt;&lt;br /&gt;";
          $format .= "This is a profile formatted as a PDF.&lt;br /&gt;";
          $format .= 'Name: ' . $Profile-&gt;name . '&lt;br /&gt;';
  
          if ($Profile instanceof StudentProfile) {
              $format .= "Graduation Date: " . $Profile-&gt;graduationDate . "&lt;br /&gt;";
          }
  
          $format .= "&lt;br /&gt;End of PDF file";
          return $format;
      }
  }
  </code></pre>
  
  <p>File 3. Profile.php</p>
  
  <pre><code>class Profile
  {
      public $name;
  
      public function __construct($name)
      {
          $this-&gt;name = $name;
      }
  
      public function format(FormatterInterface $Formatter)
      {
          return $Formatter-&gt;format($this);
      }
  }
  </code></pre>
  
  <p>File 4. StudentProfile.php</p>
  
  <pre><code>class StudentProfile extends Profile
  {
      public $graduationDate;
  
      public function __construct($name, $graduationDate)
      {
          $this-&gt;name = $name;
          $this-&gt;graduationDate = $graduationDate;
      }
  }
  </code></pre>
  
  <p>File 5. index.php</p>
  
  <pre><code>//Assuming all files are included......
  
      $StudentProfile = new StudentProfile('Michael Conner', 55, 'Unknown, FL', 'Graduate', '1975', 'Business Management');
  
      $Profile = new Profile('Brandy Smith', 44, 'Houston, TX');
  
      $PDFFormatter = new PDFFormatter();
      echo '&lt;hr /&gt;';
      echo $StudentProfile-&gt;format($PDFFormatter);
      echo '&lt;hr /&gt;';
      echo $Profile-&gt;format($PDFFormatter);
  </code></pre>