Анон, допомогi советом, с этим куском более менее разобрался, только $conf ближе к концу не дает покоя, зачем она, если нигде не применяется??
<?php
class Conf
{
private \SimpleXMLElement $xml;
private \SimpleXMLElement $lastmatch;
public function __construct(private string $file)
{
if (! file_exists($file)) {
throw new \Exception("file '{$file}' does not exist");
}
$this->xml = $xml = simplexml_load_file($file);
}
public function write(): void
{
if (! is_writable($this->file)) {
throw new \Exception("file '{$this->file}' is not writable");
}
print "{$this->file} is apparently writable <br>";
file_put_contents($this->file, $this->xml->asXML());
}
public function get(string $str): ?string
{
$matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");
if (count($matches)) {
$this->lastmatch = $matches[0];
return (string)$matches[0];
}
return null;
}
public function set(string $key, string $value): void
{
if (! is_null($this->get($key))) {
$this->lastmatch[0] = $value;
return;
}
$conf = $this->xml->conf; // что делает эта переменная?
$this->xml->addChild('item', $value)->addAttribute('name', $key);
}
}