title: Extending DOMElement in PHP with registerNodeClass
body: <p><code>registerNodeClass</code> is great for extending the various DOMNode-based DOM classes in PHP, but I need to go one level deeper.</p>
  
  <p>I've created an <code>extDOMElement</code> that extends <code>DOMElement</code>. This works great with <code>registerNodeClass</code>, but I would like to have something that works more like this:
  <code>registerNodeClass("DOMElement-&gt;nodeName='XYZ'", 'extDOMXYZElement')</code></p>
  
  <p>Consider the following XML document, animals.xml:</p>
  
  <pre><code>&lt;animals&gt;
      &lt;dog name="fido" /&gt;
      &lt;dog name="lucky" /&gt;
      &lt;cat name="scratchy" /&gt;
      &lt;horse name="flicka" /&gt;
  &lt;/animals&gt;
  </code></pre>
  
  <p>Consider the following code:</p>
  
  <pre><code>extDomDocument extends DOMDocument {
      public function processAnimals() {
          $animals = $this-&gt;documentElement-&gt;childNodes;
          foreach($animals as $animal) {
              $animal-&gt;process();
          }
      }
  }
  
  
  extDOMElement extends DOMElement {
      public function process() {
          if ($this-&gt;nodeName=='dog'){
              $this-&gt;bark();
          } elseif ($this-&gt;nodeName=='cat'){
              $this-&gt;meow();
          } elseif  ($this-&gt;nodeName=='horse'){
              $this-&gt;whinny();
          }
          this-&gt;setAttribute('processed','true');
      }
      private function bark () {
          echo "$this-&gt;getAttribute('name') the $this-&gt;nodeName barks!";
      }
      private function meow() {
          echo "$this-&gt;getAttribute('name') the $this-&gt;nodeName meows!";
      }
      private function whinny() {
          echo "$this-&gt;getAttribute('name') the $this-&gt;nodeName whinnies!";
      }
  }
  
  $doc = new extDOMDocument();
  $doc-&gt;registerNodeClass('DOMElement', 'extDOMElement');
  $doc-&gt;loadXMLFile('animals.xml');
  $doc-&gt;processAnimals();
  $doc-&gt;saveXMLFile('animals_processed_' . Now() . '.xml');
  </code></pre>
  
  <p>Output:
  fido the dog barks!
  lucky the dog barks!
  scratchy the cat meows!
  flicka the horse whinnies!</p>
  
  <p>I don't want to have to put bark(), meow() and whinny() into extDOMElement - I want to put them into extDOMDogElement, extDOMCatElement and extDOMHorseElement, respectively.</p>
  
  <p>I've looked at the Decorator and Strategy patterns here, but I'm not exactly sure how to proceed. The current setup works OK, but I'd prefer to have shared properties and methods in <code>extDOMElement</code> with separate classes for each ElementName, so that I can separate methods and properties specific to each Element out of the main classes.</p>