1 | <?php declare(strict_types=1); |
||
24 | class XML { |
||
25 | |||
26 | /** |
||
27 | * XML representation of the data |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $xml; |
||
32 | |||
33 | /** |
||
34 | * PHP array version of the data |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $data; |
||
39 | |||
40 | /** |
||
41 | * XML constructor |
||
42 | */ |
||
43 | public function __construct(string $xml = '', array $data = []) |
||
47 | |||
48 | /** |
||
49 | * Serialize the data to an xml string |
||
50 | */ |
||
51 | public function __toString(): string |
||
55 | |||
56 | /** |
||
57 | * Get the data parsed from the XML |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | public function getData(): array |
||
65 | |||
66 | /** |
||
67 | * Set the data to create xml from |
||
68 | * |
||
69 | * @param array $data |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function setData(array $data): self |
||
77 | |||
78 | /** |
||
79 | * Get the xml created from the data |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function getXML(): string |
||
87 | |||
88 | /** |
||
89 | * Set the xml to parse the data from |
||
90 | * |
||
91 | * @param string $xml |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function setXML(string $xml): self |
||
99 | |||
100 | /** |
||
101 | * Parse an xml document string to a php array |
||
102 | * |
||
103 | * @param string $xml |
||
104 | * @return array |
||
105 | */ |
||
106 | public static function toArray(string $xml): array |
||
125 | |||
126 | /** |
||
127 | * Transform the array into XML |
||
128 | * |
||
129 | * @param array $data |
||
130 | * @return string |
||
131 | */ |
||
132 | public static function toXML(array $data): string |
||
141 | |||
142 | /** |
||
143 | * Parse the xml document string to a php array |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | public function parse(): array |
||
153 | |||
154 | /** |
||
155 | * Transform the array into XML |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | public function createXML(): string |
||
163 | |||
164 | private static function stripXMLWhitespace(string $xml): string |
||
173 | |||
174 | /** |
||
175 | * Recursively create array structure based on xml structure |
||
176 | * |
||
177 | * @param array &$root A reference to the current array location |
||
178 | * @param DOMNodeList $nodeList The current NodeList object |
||
179 | * @return void |
||
180 | */ |
||
181 | private static function childNodesToArray(array &$root, DOMNodelist $nodeList) |
||
226 | |||
227 | /** |
||
228 | * Recursively create xml nodes from array properties |
||
229 | * |
||
230 | * @param DOMDocument $dom The current DOM object |
||
231 | * @param DOMNode $parent The parent element to append children to |
||
232 | * @param array $data The data for the current node |
||
233 | * @return void |
||
234 | */ |
||
235 | private static function arrayPropertiesToXmlNodes(DOMDocument &$dom, DOMNode &$parent, array $data) |
||
263 | } |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: