Extending DOMDocument and DOMNode: problem with return object
        Posted  
        
            by Glauber Rocha
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Glauber Rocha
        
        
        
        Published on 2010-04-04T07:18:47Z
        Indexed on 
            2010/04/04
            7:23 UTC
        
        
        Read the original article
        Hit count: 484
        
I'm trying to extend the DOMDocument class so as to make XPath selections easier. I wrote this piece of code:
class myDOMDocument extends DOMDocument {
 function selectNodes($xpath){
   $oxpath = new DOMXPath($this);
    return $oxpath->query($xpath);
  }
  function selectSingleNode($xpath){
   return $this->selectNodes($xpath)->item(0);
  }
}
These methods return a DOMNodeList and a DOMNode object, respectively. What I'd like to do now is to implement similar methods to the DOMNode objects. But obviously if I write a class (myDOMNode) that extends DOMNode, I won't be able to use these two extra methods on the nodes returned by myDOMDocument because they're DOMNode (and not myDOMNode) objects.
I'm rather a beginner in object programming, I've tried various ideas but they all lead to a dead-end.
Any hints? Thanks a lot in advance.
© Stack Overflow or respective owner