Skip to content

Commit 6bc8f7e

Browse files
committed
Fix #79065: DOM classes do not expose properties to Reflection
We add a `get_properties` handler which complements the already existing `has_property` and `read_property`handlers.
1 parent a2ed731 commit 6bc8f7e

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PHP NEWS
1616
- DOM:
1717
. Fixed bug #78221 (DOMNode::normalize() doesn't remove empty text nodes).
1818
(cmb)
19+
. Fixed bug #79065 (DOM classes do not expose properties to Reflection).
20+
(cmb)
1921

2022
- EXIF:
2123
. Fixed bug #79336 (ext/exif/tests/bug79046.phpt fails on Big endian arch).

ext/dom/php_dom.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,28 @@ static int dom_property_exists(zval *object, zval *member, int check_empty, void
410410
}
411411
/* }}} */
412412

413+
/* {{{ dom_get_properties */
414+
static HashTable *dom_get_properties(zval *object)
415+
{
416+
dom_object *obj = Z_DOMOBJ_P(object);
417+
HashTable *props = zend_std_get_properties(object);
418+
419+
if (obj->prop_handler != NULL) {
420+
zend_string *key;
421+
dom_prop_handler *hnd;
422+
423+
ZEND_HASH_FOREACH_STR_KEY_PTR(obj->prop_handler, key, hnd) {
424+
zval val;
425+
426+
if (hnd->read_func(obj, &val) == SUCCESS) {
427+
zend_hash_update(props, key, &val);
428+
}
429+
} ZEND_HASH_FOREACH_END();
430+
}
431+
return props;
432+
}
433+
/* }}} */
434+
413435
static HashTable* dom_get_debug_info_helper(zval *object, int *is_temp) /* {{{ */
414436
{
415437
dom_object *obj = Z_DOMOBJ_P(object);
@@ -602,6 +624,7 @@ PHP_MINIT_FUNCTION(dom)
602624
dom_object_handlers.get_property_ptr_ptr = dom_get_property_ptr_ptr;
603625
dom_object_handlers.clone_obj = dom_objects_store_clone_obj;
604626
dom_object_handlers.has_property = dom_property_exists;
627+
dom_object_handlers.get_properties = dom_get_properties;
605628
dom_object_handlers.get_debug_info = dom_get_debug_info;
606629

607630
memcpy(&dom_nnodemap_object_handlers, &dom_object_handlers, sizeof(zend_object_handlers));

ext/dom/tests/bug79065.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #79065 (DOM classes do not expose properties to Reflection)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('dom')) die('skip dom extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$dom = new DOMDocument;
10+
$dom->loadHTML('<b>test</b>');
11+
var_dump(count(get_object_vars($dom)));
12+
13+
$ro = new ReflectionObject($dom);
14+
var_dump(count($ro->getProperties()));
15+
var_dump($ro->hasProperty("textContent"));
16+
$rp = $ro->getProperty("textContent");
17+
var_dump($rp);
18+
var_dump($rp->getValue($dom));
19+
?>
20+
--EXPECTF--
21+
int(35)
22+
int(35)
23+
bool(true)
24+
object(ReflectionProperty)#%d (2) {
25+
["name"]=>
26+
string(11) "textContent"
27+
["class"]=>
28+
string(11) "DOMDocument"
29+
}
30+
string(4) "test"

0 commit comments

Comments
 (0)