From: asmqb7 at gmail dot com
Operating system: Linux
PHP version: 7.4.1
Package: *XML functions
Bug Type: Bug
Bug description:Reflection/get_object_vars cannot access contents of DOM{Document,Node,Element}
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
--
Edit bug report at https://bugs.php.net/bug.php?id=79065&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79065&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79065&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79065&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79065&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79065&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79065&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79065&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79065&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79065&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79065&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79065&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79065&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79065&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79065&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79065&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79065&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79065&r=mysqlcfg