php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79065 Reflection/get_object_vars cannot access contents of DOM{Document,Node,Element}
Submitted: 2020-01-05 02:49 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: asmqb7 at gmail dot com Assigned:
Status: Open Package: *XML functions
PHP Version: 7.4.1 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-01-05 02:49 UTC] asmqb7 at gmail dot com
Description:
------------
Known related (may be more):

- Bug 48527 - DOM XML classes do not expose properties to Reflection  (5.2,   2009)
- Bug 65565 - var_dump of DOM objects displays empty objects          (5.4,   2013)
- Bug 77685 - Property reflection fails for DOMElement::$firstChild   (7.3.2, 2019)

I can't access the contents of DOMNodes, DOMElements, DOMDocuments, and potentially other classes in the DOM/XML namespaces and/or other extensions, using either reflection or php_object_vars().

- ReflectionObject->getProperties() returns an empty array

- php_object_vars() returns an empty array

- **Sanity checks involving ReflectionObject->hasProperty() return TRUE, while followup ReflectionObject->getProperty() cause ReflectionException**

As an aside, I'm curious to learn how one might [face the music and] generally check for reflection accessibility issues in other extensions or parts of PHP.

FWIW, I haven't used print_r() or var_dump() in years. My own d() function is 5-7 chars less to type, literalizes its arguments (`d($abc, 1+2);` might show '$abc: "hello world" (11),  1+2: 3'), turns 2D arrays into tables, line-wraps automatically, uses xterm's 256 color palette (which works everywhere) for nicer CLI support, etc. Others likely have even more sophisticated routines they've developed over the years. So I'm very interested to learn how PHP code can introspect itself equivalently (or better than) print_r()/var_dump().

I would be interested to learn about hacks and workarounds for PHP versions prior to whenever this is fixed (TIA) which might let me escape the insanity of parsing `print_r(..., true)`, or burrowing around blindly with FFI (and incidentally it's going to be interesting seeing the effects of access to the Universal Hammerâ„¢ when nothing else is made possible by the language itself...).


Test script:
---------------
$dom = new DOMDocument;
$data = '<b>test</b>';
print '$dom: '; print_r($dom);
print 'get_object_vars(): '; print_r(get_object_vars($dom));
$r = new ReflectionObject($dom);
print '$r->getProperties(): '; print_r($r->getProperties());
print '$r->getStaticProperties(): '; print_r($r->getStaticProperties());
print '$r->hasProperty("textContent"): '; var_dump($r->hasProperty("textContent"));
print '$r->getProperty("textContent"): '; var_dump($r->getProperty("textContent"));

Expected result:
----------------
Script-parseable results from reflection and possibly(?) php_object_vars() that are appropriately equivalent to the text obtained by print_r()

Actual result:
--------------
$dom: DOMDocument Object
(
    [doctype] => 
    [implementation] => (object value omitted)
    [documentElement] => 
    [actualEncoding] => 
    [encoding] => 
    [xmlEncoding] => 
    [standalone] => 1
    [xmlStandalone] => 1
    [version] => 1.0
    [xmlVersion] => 1.0
    [strictErrorChecking] => 1
    [documentURI] => 
    [config] => 
    [formatOutput] => 
    [validateOnParse] => 
    [resolveExternals] => 
    [preserveWhiteSpace] => 1
    [recover] => 
    [substituteEntities] => 
    [nodeName] => #document
    [nodeValue] => 
    [nodeType] => 9
    [parentNode] => 
    [childNodes] => (object value omitted)
    [firstChild] => 
    [lastChild] => 
    [previousSibling] => 
    [nextSibling] => 
    [attributes] => 
    [ownerDocument] => 
    [namespaceURI] => 
    [prefix] => 
    [localName] => 
    [baseURI] => 
    [textContent] => 
)
get_object_vars(): Array
(
)
$r->getProperties(): Array
(
)
$r->getStaticProperties(): Array
(
)
$r->hasProperty("textContent"): bool(true)
$r->getProperty("textContent"): PHP Fatal error:  Uncaught ReflectionException: Property textContent does not exist in Standard input code:10
Stack trace:
#0 Standard input code(10): ReflectionClass->getProperty('textContent')
#1 {main}
  thrown in Standard input code on line 10


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Feb 25 13:01:25 2020 UTC