DOMNode to DOMElement in php

后端 未结 5 1460
南旧
南旧 2021-02-13 20:15

I want to convert a DOMNode object from a call to getElementsByTagName() to a DOMElement in order to access methods like getElements

5条回答
  •  无人及你
    2021-02-13 21:04

    If anyone is looking for a more elegant solution in IntelliJ/PHPStorm. I managed to solve the issue like this:

        private function doSomething(DOMNode $child){
            
            if(!$child instanceof DOMElement)
                throw new Exception('DOMElement expected but got something else.');
            
            $attr = $child->getAttribute('attr'); //recognized by PHPStorm now as DOMElement
            
            /* and more code */
            
        }
    

    All my code corretly assumes DOMNode which is de super class of DOMElement but also DOMComment etc. When I need DOMElement to use getAttribute for example, then I add above check.

提交回复
热议问题