PHP 8.0: XMLParser
objects replace xml
resources
As part of PHP's resource
to object migration, XML Parser extensions primary resource
types are migrated to XMLParser
class objects in PHP 8.0.
Prior to PHP 8.0, xml_parser_create
and xml_parser_create_ns
functions returned a resource
of type xml
. In PHP 8.0, all XML Parser functions return and accept XMLParser
objects.
XMLParser
class synopsis
final XMLParser {}
xml_parser_create
and xml_parser_create_ns
functions must be used to instantiate XMLParser
objects. Directly instantiating with new XMLParser
construct is not allowed, and results in an error:
new XMLParser();
PHP Error: Cannot directly construct XmlParser, use xml_parser_create() or xml_parser_create_ns() instead in ... on line ...
is_resource
calls
The return values of xml_parser_create
and xml_parser_create_ns
functions are standard PHP class objects, and are no longer resource
objects.
is_resource
function no longer returns true
for the return values of said functions.
Destroying XMLParser
objects
It is no longer necessary to call xml_parser_free
that was required in previous PHP versions to avoid memory leaks.
xml_parser_free
function is not deprecated, and calling it (for backwards-compatibility) will not cause any issues.
Note that it may be necessary to explicitly destroy XML parser objects (unset($parser)
) in case the objects might not fall out of scope for the garbage collector to clean them. PHP 8's Weak Maps might help to associate additional data for XML Parser objects without objecting the garbage collector.
Backwards Compatibility Impact
Unless there are is_resource
calls, there should be no backwards-compatibility issues. In PHP 8.0, all XML Parser functions accept XMLParser
objects instead of resource
objects.