Bug #79065 [Opn->Ver]: DOM classes do not expose properties to Reflection
| From: | cmb@php.net | Date: | Tue, 21 Apr 2020 11:33:59 +0000 |
| Subject: | Bug #79065 [Opn->Ver]: DOM classes do not expose properties to Reflection | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226701@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79065&edit=1
ID: 79065
Updated by: cmb@php.net
Reported by: asmqb7 at gmail dot com
-Summary: Reflection/get_object_vars cannot access contents of
DOM{Document,Node,Element}
+Summary: DOM classes do not expose properties to Reflection
-Status: Open
+Status: Verified
Type: Bug
-Package: *XML functions
+Package: DOM XML related
Operating System: Linux
PHP Version: 7.4.1
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2020-04-21 11:32:56] cmb@php.net
Related To: Bug #77685
------------------------------------------------------------------------
[2020-01-05 02:49:58] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79065&edit=1