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

前端 未结 2 1236
孤独总比滥情好
孤独总比滥情好 2020-11-27 21:54

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

相关标签:
2条回答
  • 2020-11-27 22:06

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


    It's a bug:

    • http://bugs.php.net/bug.php?id=48527
    0 讨论(0)
  • 2020-11-27 22:22

    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 讨论(0)
提交回复
热议问题