xref: /PHP-8.4/ext/dom/tests/DOMElement_id.phpt (revision 72e2e250)
1--TEST--
2DOMElement::id
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8class MyStringable {
9    public function __toString(): string {
10        throw new Exception("foo");
11    }
12}
13
14$dom = new DOMDocument();
15$dom->loadXML('<html><div/></html>');
16$div = $dom->documentElement->firstChild;
17
18var_dump($div->id);
19$div->id = "hello & world<>";
20var_dump($div->id);
21$div->id = "";
22var_dump($div->id);
23$div->id = "é";
24var_dump($div->id);
25$div->id = "\0";
26var_dump($div->id);
27$div->id = 12345;
28var_dump($div->id);
29try {
30    $div->id = new MyStringable();
31} catch (Throwable $e) {
32    echo "Error: ", $e->getMessage(), "\n";
33}
34var_dump($div->id);
35echo $dom->saveXML();
36
37var_dump($dom->getElementById("12345") === $div);
38
39?>
40--EXPECT--
41string(0) ""
42string(15) "hello & world<>"
43string(0) ""
44string(2) "é"
45string(0) ""
46string(5) "12345"
47Error: foo
48string(5) "12345"
49<?xml version="1.0"?>
50<html><div id="12345"/></html>
51bool(true)
52