XML::offsetGet()

Gets the attribute’s value.

Table of Contents
  1. Description
  2. Example

Description

XML::offsetGet(mixed $key): ?string;

This method is a requirement to be able to implement the ArrayAccess interface and aims to make the object data accessible using array access syntax. You won’t use this method directly, it will be executed automatically when you try to get an attribute’s value with array access syntax. If the attribute does not exist in the node, this method returns null.

Passing a numeric key is considered as getting the data directly from the source. Which means that calling attribute 0 returns the node name, calling attribute 1 returns the node content (if node is void, this attribute returns false), calling attribute 2 returns the node attributes.

Example

$node = new XML('<input disabled="disabled" name="test" value="aaa&quot;bbb"/>');

test($node['asdf']); // Returns `null` because attribute `asdf` does not exist
test($node['disabled']); // Returns `true` because attribute `disabled` does exist
test($node['name']); // Returns `'test'` because attribute `name` does exist
test($node['value']); // Returns `'aaa"bbb'` because attribute `value` does exist
test($node[0]); // Returns `'input'`
test($node[1]); // Returns `false` because node is void
test($node[2]); // Returns `['disabled' => true, 'name' => 'test', 'value' => 'aaa"bbb']`

XML::offsetGet()

Gets the attribute’s value.