diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c index 3e90db31db7e3..29adecd8dcbde 100644 --- a/ext/dom/php_dom.c +++ b/ext/dom/php_dom.c @@ -487,6 +487,23 @@ static HashTable* dom_get_debug_info(zend_object *object, int *is_temp) /* {{{ * } /* }}} */ +static HashTable *dom_get_properties_for(zend_object *obj, zend_prop_purpose purpose) +{ + switch (purpose) { + case ZEND_PROP_PURPOSE_ARRAY_CAST: + zend_throw_error(NULL, "%s cannot be cast to an array because the properties are virtual and do not have a raw value", ZSTR_VAL(obj->ce->name)); + return NULL; + case ZEND_PROP_PURPOSE_JSON: + zend_throw_error(NULL, "%s cannot be encoded to JSON because the properties are virtual and do not have a raw value", ZSTR_VAL(obj->ce->name)); + return NULL; + case ZEND_PROP_PURPOSE_VAR_EXPORT: + zend_throw_error(NULL, "%s cannot be exported because the representation would be insufficient to restore the object from", ZSTR_VAL(obj->ce->name)); + return NULL; + default: + return zend_std_get_properties_for(obj, purpose); + } +} + void *php_dom_export_node(zval *object) /* {{{ */ { php_libxml_node_object *intern; @@ -695,6 +712,7 @@ PHP_MINIT_FUNCTION(dom) dom_object_handlers.clone_obj = dom_objects_store_clone_obj; dom_object_handlers.has_property = dom_property_exists; dom_object_handlers.get_debug_info = dom_get_debug_info; + dom_object_handlers.get_properties_for = dom_get_properties_for; memcpy(&dom_modern_domimplementation_object_handlers, &dom_object_handlers, sizeof(zend_object_handlers)); /* The IDL has the [SameObject] constraint, which is incompatible with cloning because it imposes that there is only diff --git a/ext/dom/tests/DOMDocument_json_encode.phpt b/ext/dom/tests/DOMDocument_json_encode.phpt deleted file mode 100644 index ed85ab4f2109e..0000000000000 --- a/ext/dom/tests/DOMDocument_json_encode.phpt +++ /dev/null @@ -1,11 +0,0 @@ ---TEST-- -JSON encoding a DOMDocument ---EXTENSIONS-- -dom ---FILE-- - ---EXPECT-- -{} diff --git a/ext/dom/tests/get_properties_for.phpt b/ext/dom/tests/get_properties_for.phpt new file mode 100644 index 0000000000000..40650dd196f03 --- /dev/null +++ b/ext/dom/tests/get_properties_for.phpt @@ -0,0 +1,28 @@ +--TEST-- +Get properties for DOM nodes +--EXTENSIONS-- +dom +--FILE-- +getMessage(), "\n"; +} +try { + (array) $doc; +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} +try { + var_export($doc); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} +?> +--EXPECT-- +DOMDocument cannot be encoded to JSON because the properties are virtual and do not have a raw value +DOMDocument cannot be cast to an array because the properties are virtual and do not have a raw value +\DOMDocument::__set_state(array( +))DOMDocument cannot be exported because the representation would be insufficient to restore the object from