为什么var_dump
不能处理DOMDocument对象,而print($dom->saveHTML())
可以产生输出?
发布于 2011-01-24 04:53:45
从PHP5.4.1开始,你终于可以var_dump
DOM对象了。请参阅https://gist.github.com/2499678
这是一个bug:
发布于 2011-01-24 04:11:41
它与任何接口无关,实际上非常简单。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(...)
例如,类异常的属性在文件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);
然后所有这些函数都会调用
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, const char *name, ...
这将更新属性列表。然后是ext/standard/var.c
中的var_dump
,并通过调用php_object_property_dump
来查找它们,后者通过相同的属性列表枚举它们。您可以看到有意公开的内部结构的。
DOM扩展just 的开发人员选择不公开的类结构。它们根本不调用这些类型的函数。这就是你什么也看不见的原因。
object(DOMDocument)#1 (0) {
}
如果你查看ext/dom/php_dom.c
,你会发现一次属性声明。它是为DomException类准备的。它重新定义了属性code
。
zend_declare_property_long(dom_domexception_class_entry, "code", ...
如果异常转储看起来像
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
}
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)
}
看到code属性是如何移动到末尾的吗?这是因为重新声明。
https://stackoverflow.com/questions/4776093
复制相似问题