为什么var_dump不能与DomDocument对象一起使用,而print($ dom-> saveHTML())呢?

时间:2022-04-29 13:25:08

Why doesn't var_dump work with DomDocument objects, while print($dom->saveHTML()) produces output?

为什么var_dump不能与DomDocument对象一起使用,而print($ dom-> saveHTML())会产生输出?

2 个解决方案

#1


9  

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

更新:从PHP 5.4.1开始,您最终可以使用var_dump DOM对象。请参阅https://gist.github.com/2499678


It's a bug:

这是一个错误:

#2


6  

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

它与任何接口无关,实际上非常简单。 var_dump仅显示由开发人员通过调用此类C函数声明的那些类属性

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

例如,类Exception的属性在Zend / zend_exceptions.c文件中声明

   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.

它会更新属性列表。然后是ext / standard / var.c中的var_dump,并通过调用php_object_property_dump查找它们,它通过相同的属性列表枚举它们。你看到有意暴露的内部结构。

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.

DOM扩展的开发人员只是选择不公开其类的结构。他们根本就没有调用那些功能。这就是你什么也看不见的原因。

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.

如果你查看ext / dom / php_dom.c,你会发现一次属性声明。它适用于DomException类。它重新定义了属性代码。

zend_declare_property_long(dom_domexception_class_entry, "code", ...

If the Exception dump looks like

如果Exception转储看起来像

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.

DOMException转储有点不同。

 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.

看看代码属性如何移动到最后?这是因为重新声明。

#1


9  

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

更新:从PHP 5.4.1开始,您最终可以使用var_dump DOM对象。请参阅https://gist.github.com/2499678


It's a bug:

这是一个错误:

#2


6  

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

它与任何接口无关,实际上非常简单。 var_dump仅显示由开发人员通过调用此类C函数声明的那些类属性

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

例如,类Exception的属性在Zend / zend_exceptions.c文件中声明

   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.

它会更新属性列表。然后是ext / standard / var.c中的var_dump,并通过调用php_object_property_dump查找它们,它通过相同的属性列表枚举它们。你看到有意暴露的内部结构。

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.

DOM扩展的开发人员只是选择不公开其类的结构。他们根本就没有调用那些功能。这就是你什么也看不见的原因。

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.

如果你查看ext / dom / php_dom.c,你会发现一次属性声明。它适用于DomException类。它重新定义了属性代码。

zend_declare_property_long(dom_domexception_class_entry, "code", ...

If the Exception dump looks like

如果Exception转储看起来像

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.

DOMException转储有点不同。

 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.

看看代码属性如何移动到最后?这是因为重新声明。