php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79065 DOM classes do not expose properties to Reflection
Submitted: 2020-01-05 02:49 UTC Modified: 2024-09-10 18:41 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: asmqb7 at gmail dot com Assigned: nielsdos (profile)
Status: Closed Package: DOM XML related
PHP Version: 7.4.1 OS: Linux
Private report: No CVE-ID: None
 [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

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-21 11:33 UTC] cmb@php.net
-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 -Package: *XML functions +Package: DOM XML related -Assigned To: +Assigned To: cmb
 [2020-04-21 11:36 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #79065: DOM classes do not expose properties to Reflection
On GitHub:  https://github.com/php/php-src/pull/5428
Patch:      https://github.com/php/php-src/pull/5428.patch
 [2020-04-27 08:29 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=6bc8f7e5a9949b2ba79376abd1ed13d0b4d0ae3c
Log: Fix #79065: DOM classes do not expose properties to Reflection
 [2020-04-27 08:29 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2020-04-28 13:10 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=75470bc312ef2e151696ea5c1ee026c089944623
Log: Revert &quot;Fix #79065: DOM classes do not expose properties to Reflection&quot;
 [2020-04-28 13:27 UTC] cmb@php.net
-Status: Closed +Status: Re-Opened -Type: Bug +Type: Documentation Problem -Assigned To: cmb +Assigned To:
 [2020-04-28 13:27 UTC] cmb@php.net
The "fix" was wrong, and has been reverted.  Re-opening and
changing to documentation problem, so it can be documented that
the properties are actually overloaded (ala __get()).
 [2024-09-10 18:41 UTC] nielsdos@php.net
-Status: Re-Opened +Status: Closed -Type: Documentation Problem +Type: Bug -Assigned To: +Assigned To: nielsdos
 [2024-09-10 18:41 UTC] nielsdos@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at
http://www.php.net/downloads.php

This was fixed in PHP 8.1 as a consequence of declaring the properties in the stub files (Thanks Máté :))
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC