PHP xml_set_object() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
XML Parser

Definition

In PHP, xml_set_object() lets an XML parser use the methods of an object as handlers, so you can name them with plain strings such as 'start'.

After you call it, handler names given as strings to functions like xml_set_element_handler() are looked up as methods on that object. This function is deprecated as of PHP 8.4. Modern code passes callable arrays such as [$this, 'start'] instead, which needs no xml_set_object() call.

Syntax

Call xml_set_object() with the values you need. Start with this line.

PHP
xml_set_object($parser, $this);
xml_set_element_handler($parser, 'start', 'end');
CallWhat it does
xml_set_object($parser, $this)Makes string handler names refer to methods of the current object.
xml_set_object($parser, $reader)Makes string handler names refer to methods of the $reader object.
xml_set_element_handler($parser, [$this, 'start'], [$this, 'end'])The modern alternative that needs no xml_set_object() call.

Handler methods are called as method(XMLParser $parser, ...), just like plain functions, and should be public. Handler names given as strings are only treated as methods after xml_set_object() was called. Calling this function triggers a deprecation notice in PHP 8.4 and later.

Library declarations
PHP
xml_set_object(XMLParser $parser, object $object): true

Parameters

ParameterDescription
parserRequired. The XMLParser object created by xml_parser_create() or xml_parser_create_ns().
objectRequired. The object whose methods should be used as handlers.

Return Value

Return ValueDescription
trueAlways returns true after the object is attached to the parser.

PHP Examples

Example 1 — A Reader Class

Wrap the parser in a class, attach the object with xml_set_object(), and use method names as string handlers.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

class BookReader
{
    public array $titles = [];
    private string $tag = '';

    public function parse(string $xml): array
    {
        $parser = xml_parser_create();
        xml_set_object($parser, $this);
        xml_set_element_handler($parser, 'start', 'end');
        xml_set_character_data_handler($parser, 'text');
        xml_parse($parser, $xml, true);
        return $this->titles;
    }

    public function start(XMLParser $parser, string $name, array $attrs): void
    {
        $this->tag = $name;
    }

    public function end(XMLParser $parser, string $name): void
    {
        $this->tag = '';
    }

    public function text(XMLParser $parser, string $data): void
    {
        if ($this->tag === 'BOOK') {
            $this->titles[] = $data;
        }
    }
}

$reader = new BookReader();
$titles = $reader->parse('<shelf><book>PHP</book><book>XML</book></shelf>');
echo 'Titles: ', implode(', ', $titles), PHP_EOL;

How It Works

  1. error_reporting() hides the PHP 8. 4 deprecation notice so the output stays clean.
  2. xml_set_object() attaches the BookReader object to the parser.
  3. The strings start, end, and text now refer to methods of that object.
  4. The text() method keeps only the text found inside book tags and stores it in the titles array.

Example 2 — One Object per Parser

Each parser can have its own object, so two parsers keep their own separate state.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

class TagCounter
{
    public int $count = 0;

    public function start(XMLParser $parser, string $name, array $attrs): void
    {
        $this->count++;
    }

    public function end(XMLParser $parser, string $name): void
    {
    }
}

function countTags(string $xml, TagCounter $counter): void
{
    $parser = xml_parser_create();
    xml_set_object($parser, $counter);
    xml_set_element_handler($parser, 'start', 'end');
    xml_parse($parser, $xml, true);
}

$first = new TagCounter();
$second = new TagCounter();

countTags('<a><b/></a>', $first);
countTags('<a/>', $second);

echo 'First counter: ', $first->count, PHP_EOL;
echo 'Second counter: ', $second->count, PHP_EOL;

How It Works

  1. Two TagCounter objects are created, and each one starts with a count of 0.
  2. countTags() builds a new parser and attaches the object it was given with xml_set_object().
  3. The first document has two tags, so the first object counts 2 start calls.
  4. The second document has one tag, so the second object counts 1 and the two states never mix.

Example 3 — The Modern Callable Array Way

Callable arrays give the same result without xml_set_object(), so they also avoid the deprecation notice.

PHP
<?php
class BookReader
{
    public array $titles = [];
    private string $tag = '';

    public function parse(string $xml): array
    {
        $parser = xml_parser_create();
        xml_set_element_handler($parser, [$this, 'start'], [$this, 'end']);
        xml_set_character_data_handler($parser, [$this, 'text']);
        xml_parse($parser, $xml, true);
        return $this->titles;
    }

    public function start(XMLParser $parser, string $name, array $attrs): void
    {
        $this->tag = $name;
    }

    public function end(XMLParser $parser, string $name): void
    {
        $this->tag = '';
    }

    public function text(XMLParser $parser, string $data): void
    {
        if ($this->tag === 'BOOK') {
            $this->titles[] = $data;
        }
    }
}

$reader = new BookReader();
$titles = $reader->parse('<shelf><book>PHP</book><book>XML</book></shelf>');
echo 'Titles: ', implode(', ', $titles), PHP_EOL;

How It Works

  1. The class is the same as before, but there is no call to xml_set_object().
  2. Each handler is passed as an array holding the object and the method name.
  3. PHP can call [$this, 'start'] directly, so the parser needs no extra object setup.
  4. The output matches the xml_set_object() version, which is why callable arrays are recommended.

Frequently Asked Questions

It attaches an object to an XML parser, so handler names given as strings are treated as methods of that object.
Yes, as of PHP 8.4. Use callable arrays such as [$this, 'start'] or closures instead. They work in every PHP 8 version and do not need xml_set_object().
Making them public is the safest choice. The parser calls them from outside your class, so private methods can fail to be found.
You do not need it. A closure is already callable, so you can pass it directly to xml_set_element_handler() and the other handler functions.
It returns true in PHP 8. The call only stores the object, so there is no error value to check.

Next: xml_set_processing_instruction_handler()

xml_set_processing_instruction_handler() registers the function that runs when the XML par.

xml_set_processing_instruction_handler() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful