xml_set_object()

xml_set_object() — Use XML Parser within an object.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

The PHP manual marks this function as deprecated (since PHP 8.4, provide a proper method callable to xml_set_*_handler() functions): do not use it in new code.

What it does

This function allows to use parser inside object. All callback functions could be set with xml_set_element_handler() etc and assumed to be methods of object.

Signature

xml_set_object(XMLParser $parser, object $object): true

Parameters

ParameterTypeWhat it takes
$parserXMLParserA reference to the XML parser to use inside the object.
$objectobjectThe object where to use the XML parser.

Return value

Always returns true.

Example

A runnable snippet:

class CustomXMLParser { private $parser; function __construct() { $this->parser = xml_parser_create(); xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, "tag_open", "tag_close"); xml_set_character_data_handler($this->parser, "cdata"); } function parse($data) { xml_parse($this->parser, $data); } function tag_open($parser, $tag, $attributes) { var_dump($tag, $attributes); } function cdata($parser, $cdata) { var_dump($cdata); } function tag_close($parser, $tag) { var_dump($tag); } } $xml_parser = new CustomXMLParser(); $xml_parser->parse("<A ID='hallo'>PHP</A>");

Output:

string(1) "A" array(1) { ["ID"]=> string(5) "hallo" } string(3) "PHP" string(1) "A"

Changelog

VersionChange
8.4.0This function is now deprecated, instead pass proper callable values to xml_set_()
8.0.0parser expects an XMLParser instance now; previously, a valid xml resource was expected.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page xml_set_object() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.