Why doesn't var_dump
work with DOMDocument objects, while print($dom->saveHTML())
produces output?
2 Answers
Update: As of PHP 5.4.1 you can finally var_dump
DOM objects. See https://gist.github.com/2499678
It's a bug:
-
@akond like I said in my answer (before the edit), I am bad at reading C Code, but you are probably right with the explanation. Removed my guess about Traversable. It's still a bug though. – Gordon Feb 24 '11 at 23:05
It has nothing to do with any interfaces and in fact is very simple. var_dump shows only those class properties that have been declared by their developers by calling such C-functions as
ZEND_API int zend_declare_property(...)
ZEND_API int zend_declare_property_null(...)
ZEND_API int zend_declare_property_bool(...)
ZEND_API int zend_declare_property_long(...)
ZEND_API int zend_declare_property_double(...)
ZEND_API int zend_declare_property_string(...)
ZEND_API int zend_declare_property_stringl(...)
For instance, the properties of the class Exception are declared in the file Zend/zend_exceptions.c like this
zend_declare_property_string(default_exception_ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_string(default_exception_ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_long(default_exception_ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(default_exception_ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(default_exception_ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(default_exception_ce, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(default_exception_ce, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
All these functions then call
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, const char *name, ...
which updates the property list. Then comes the var_dump
in ext/standard/var.c
and looks them up by calling php_object_property_dump
which enumerates them through the same property list. You see the intentionally exposed inner structure.
The developers of the DOM extension just have chosen not to expose the structure of their classes. They simply do not call those sort of functions. That is why you see nothing.
object(DOMDocument)#1 (0) {
}
If you look into ext/dom/php_dom.c
you find a property declaration once. And it's for the DomException class. It redefines the property code
.
zend_declare_property_long(dom_domexception_class_entry, "code", ...
If the Exception dump looks like
var_dump (new Exception ('test', 102));
object(Exception)#1 (7) {
["message":protected]=>
string(4) "test"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(102)
["file":protected]=>
string(37) "/usr/local/www/apache22/data/dump.php"
["line":protected]=>
int(3)
["trace":"Exception":private]=>
array(0) {
}
["previous":"Exception":private]=>
NULL
}
The DOMException dump is a little bit different.
var_dump (new DOMException ());
object(DOMException)#2 (7) {
["message":protected]=>
string(0) ""
["string":"Exception":private]=>
string(0) ""
["file":protected]=>
string(37) "/usr/local/www/apache22/data/dump.php"
["line":protected]=>
int(9)
["trace":"Exception":private]=>
array(0) {
}
["previous":"Exception":private]=>
NULL
["code"]=>
int(0)
}
See how the code property moved to the end? It's because of redeclaration.

- 15,865
- 4
- 35
- 55
-
-
@Juergen The question asked was "why does it behave this way?" not "how do I do X?" So this answers the question by explaining exactly why it behaves that way. There are many related "how do I ..." questions that could be asked, and they would have their own answers. – IMSoP Feb 24 '17 at 14:52