XPath クエリを簡単にするために、2 つのネイティブ PHP5 クラス (DOMDocument と DOMNode) を拡張して 2 つのメソッド (selectNodes と selectSingleNode) を実装しようとしています。かなり簡単だと思いましたが、OOPの初心者の問題だと思う問題に行き詰まっています。
class nDOMDocument extends DOMDocument {
public function selectNodes($xpath){
$oxpath = new DOMXPath($this);
return $oxpath->query($xpath);
}
public function selectSingleNode($xpath){
return $this->selectNodes($xpath)->item(0);
}
}
次に、DOMNode を拡張して同じメソッドを実装しようとしたので、ノードで直接 XPath クエリを実行できます。
class nDOMNode extends DOMNode {
public function selectNodes($xpath){
$oxpath = new DOMXPath($this->ownerDocument,$this);
return $oxpath->query($xpath);
}
public function selectSingleNode($xpath){
return $this->selectNodes($xpath)->item(0);
}
}
次に、(任意の XMLDocument で) 次のコードを実行すると:
$xmlDoc = new nDOMDocument;
$xmlDoc->loadXML(...some XML...);
$node1 = $xmlDoc->selectSingleNode("//item[@id=2]");
$node2 = $node1->selectSingleNode("firstname");
3 行目は機能し、DOMNode オブジェクト $node1 を返します。ただし、selectSingleNode メソッドは DOMNode ではなく nDOMNode クラスに属しているため、4 行目は機能しません。私の質問: 返された DOMNode オブジェクトを nDOMNode オブジェクトに「変換」する方法はありますか? ここでいくつかの重要な点が欠けているように感じます。助けていただければ幸いです。
(申し訳ありませんが、これは私の質問の言い直しです DOMDocument と DOMNode の拡張: return オブジェクトの問題)