I have wrap class for creating xml file and i dont know how to rewrite add() method to work correctly. Variable SHOPITEM should be parent element, but i dont know how to reach it in add method.
Any advice are welcome
class Items {
const XML_VERSION enter code here= '1.0';
const XML_ENCODING = 'utf-8';
const SHOP = 'SHOP';
private $xml, $xmlElement;
public function __construct() {
$this->xml = new DOMDocument(self::XML_VERSION, self::XML_ENCODING);
//$this->xml->preserveWhiteSpace = false;
$this->xml->formatOutput = true;
$this->xmlElement = $this->create(self::SHOP);
}
public function create($nodeName, $value = null) {
return $this->xml->createElement($nodeName, $value);
}
public function add($object) {
return $this->xmlElement->appendChild($object);
}
public function write() {
$this->xml->appendChild($this->xmlElement);
$this->xml->save("test.xml");
}
}
$items = new Items();
$SHOPITEM = $items->create('SHOPITEM');
$product1 = $items->create('Product1', 'Some value 1');
$product2 = $items->create('Product2', 'Some value 2');
//this wont work
$SHOPITEM->add($product1);
//this works
$items->add($product1);
$items->add($product2);
//this works
$items->add($SHOPITEM);
$items->write();