开发者

Why doesn't var_dump work with DOMDocument objects, while print($dom->saveHTML()) does?

开发者_如何学编程Why doesn't var_dump work with DOMDocument objects, while print($dom->saveHTML()) produces output?


Update: As of PHP 5.4.1 you can finally var_dump DOM objects. See https://gist.github.com/2499678


It's a bug:

  • https://bugs.php.net/bug.php?id=48527


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜