Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. Анон, допомогi советом, с этим куском более менее разобрался, только $conf ближе к концу не дает покоя, зачем она, если нигде не применяется??
  2.  
  3. <?php
  4. class Conf
  5. {
  6.     private \SimpleXMLElement $xml;
  7.     private \SimpleXMLElement $lastmatch;
  8.  
  9.     public function __construct(private string $file)
  10.     {
  11.         if (! file_exists($file)) {
  12.             throw new \Exception("file '{$file}' does not exist");
  13.         }
  14.         $this->xml = $xml = simplexml_load_file($file);
  15.     }
  16.  
  17.     public function write(): void
  18.     {
  19.         if (! is_writable($this->file)) {
  20.             throw new \Exception("file '{$this->file}' is not writable");
  21.         }
  22.         print "{$this->file} is apparently writable <br>";
  23.         file_put_contents($this->file, $this->xml->asXML());
  24.     }
  25.  
  26.     public function get(string $str): ?string
  27.     {
  28.         $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");
  29.         if (count($matches)) {
  30.             $this->lastmatch = $matches[0];
  31.             return (string)$matches[0];
  32.         }
  33.         return null;
  34.     }
  35.  
  36.     public function set(string $key, string $value): void
  37.     {
  38.         if (! is_null($this->get($key))) {
  39.             $this->lastmatch[0] = $value;
  40.             return;
  41.         }
  42.         $conf = $this->xml->conf; // что делает эта переменная?
  43.         $this->xml->addChild('item', $value)->addAttribute('name', $key);
  44.     }
  45. }