Need help with get_object_vars() function

Please help to get ‘nodeValue’ property of ‘DOMElement’ class using get_object_vars() function.

[php]foreach($paragraphList as $paragraph)
{
var_dump(property_exists($paragraph,‘nodeValue’)); // line 1
var_dump($paragraph->nodeValue); // line 2
var_dump(get_object_vars($paragraph)); // line 3
}
[/php]
Here:
$paragraphList is an object of DOMNodeList class
$paragraph is an object of DOMElement class

line 1 returns TRUE
line 2 returns a string
line 3 returns empty array(0) { } , but I expect array of properties with ‘nodeValue’ strings among others. Why so?

Thanks.

From what I understand, get_object_vars() will pull the properties of the object.

Therefore, display $paragraph first and see what is really in it.
Use print_r($paragraph);

This will show the array which is really the paragraph object. So, the issue is that your $paragraphlist
contains an array of objects. And the function get_object_vars() is defined as:
" Gets the accessible non-static properties of the given object according to scope. " (as an array)

And, what it returns is defined as:
" Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property has not been assigned a value, it will be returned with a NULL value. "

So, you can’t just print the results as it is an “associative” array. You need to tell it which part you want.

Normally as far as I know, you display the nodeValue like this:
echo $domElement->nodeValue;
This is if the $domElement is set up as a real DOMElement as you spelled it.

If $paragraph was defined as a DOM Element, then just " echo $paragraph->nodeValue; " will work…

I searched for this and found the manual at: http://php.net/manual/en/class.domelement.php
Skip down to the contributed stuff as the rest you already know…

Not sure if that helps. Hope so! Good luck, let us know…

print_r($paragraph)
outputs
DOMElement Object
(
)

$arr=get_object_vars($paragraph);
var_dump($arr[‘nodeValue’]);
outputs
NULL

Well, Andy122, If your output from the DOM object is blank, then, you can’t get properties out of it…
You said:

outputs DOMElement Object ( )
Therefore, your DOMElement Ojbect is empty. You are not creating it correctly as it is empty. So, you can't get nodes or other properties if it is empty...

Check how you are creating the DOM object…

Sponsor our Newsletter | Privacy Policy | Terms of Service