Fatal error: Call to a member function getAttribute() on a non-object
·
1 min read
For some reason I can pull out a DomElement object into a variable but try to access it's methods and I get the above error. Use it in the foreach and the DomElement object works just fine.
function pb_get_image($output){
$doc = new DOMDocument();
@$doc->loadHTML($output);
$tags = $doc->getElementsByTagName('img');
/*
$tags = object(DOMNodeList)#244 (1) { ["length"]=> int(1) }
*/
$test = $doc->getElementsByTagName('img')->item(0);
/*
$test = object(DOMElement)#242 (17) { ["tagName"]=> string(3) "img" ["schemaTypeInfo"]=> .....
*/
// $width = $test->getAttribute('height');
/* This returns:
Fatal error: Call to a member function getAttribute() on a non-object in D:sharedwpdeva01wp-contentthemespicturebloghome.php on line 38 */
echo "width = $width";
echo '$tags =';
var_dump($tags);
echo '$test =';
var_dump($test);
foreach ($tags as $tag) {
echo ' $tag =';
var_dump($tag);
/*
tag = object(DOMElement)#242 (17) { ["tagName"]=> string(3) "img" ["schemaTypeInfo"]=> ...
*/
$src = $tag->getAttribute('height'); //this works no problem!?!
var_dump($src);
}
return $output;
}