title: Polymorphism with uncooperative class hierarchy
body: <p>I have a scenario where I have several classes that extend from a common ancestor because they all share common properties and methods.  Half of these classes (Group A) contain a property that is not shared by the other half (Group B), so each class in Group A explicitly declares this property rather than the parent class.</p>
  
  <p>e.g. </p>
  
  <pre><code>class parent
  {
  
  }
  
  class child1 &lt;and child2, and child3&gt; extends parent
  {
      protected $specialProperty;
  }
  
  class child4 &lt;and child5, and child5&gt; extends parent
  {
      // no "$specialProperty"
  }
  </code></pre>
  
  <p>I have some behavior I need to implement that acts on <code>$specialProperty</code>.  Naturally, I'd like to put this in a parent class and do it only once, but <code>$specialProperty</code> doesn't exist in all of the child classes.</p>
  
  <p>One option is to create an intermediate, abstract class that declares <code>$specialProperty</code> and implements the required behavior, but I'm reluctant to do that for a few reasons:</p>
  
  <ol>
  <li>This scenario is going to be coming up more soon.  What do I do then?  Create 5 or 6 different middlemen abstract classes for each of these functionalities?</li>
  <li>The class hierarchy is already about 5 levels deep above <code>parent</code>.  Should I really be creating more and more layers to make up for a less-than-ideal class hierarchy that was conceived hastefully with limited requirements?</li>
  <li>At least as far as PHP is concerned, wouldn't so many layers of inheritance eventually lead to performance issues?</li>
  </ol>
  
  <p>Another option is to do the following:</p>
  
  <pre><code>class parent
  {
      public function functionThatTheAppWillCallAutomatically()
      {
         if ( property_exists($this, 'specialProperty') )
         {
             // do stuff with specialProperty
         }
      }
  }
  </code></pre>
  
  <p>The "stuff" would get executed by child classes that have <code>$specialProperty</code> and skipped by those that don't.  I <strong><em>hate</em></strong> this solution though because it just seems wrong and sloppy to me.  In my opinion, the parent shouldn't be deciding what to do based on the properties of the child (heck, it probably shouldn't even know that the child exists - isn't that the reason for the child class in the first place?)</p>
  
  <p>In short, I'm not sure which of these two options is the least bad, or if there's a better possibility.  I'm anxiously awaiting suggestions.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I ended up doing the following to implement this specialized functionality.</p>
  
  <pre><code>class archivalDecorator extends decoratorBase /* decoratorBase just has constructor and the object property */
  {
      public function archive()
      {
          if ( !$this-&gt;object-&gt;archive() )
          {
              return false;
          }
  
          if ( property_exists($this-&gt;object, 'specialProperty') )
          {
              // do extra stuff here that involves "specialProperty"
          }
  
          return true;
      }
  }
  </code></pre>
  
  <p>This way, all of my objects perform the same archival workflow, but the classes that require special behavior can still perform it without me needing to implement dozens of subclasses in the hierarchy for these special cases.</p>
  
  <p>Though I'm still using <code>property_exists(...)</code> to determine if the special behavior is needed or not, I think this is OK now because I'm not doing it from within the parent class.  <code>$specialProperty</code> is a public property, so there's no reason external classes shouldn't know about it, but something just felt wrong about the parent class checking for that property in a child class using <code>$this</code>.</p>
  
  <p>I hope I haven't misapplied the concept.</p>