name = $name; $this->cost = $cost; $this->icon = $icon; $this->desc = $desc; $this->feat = $feat; } /** * Applies Item's features on the player. * * @return void */ function consume() { foreach($this->feat as $k => $v) { switch($k) { case "hp": increasePerso("hp", +$v); break; case "power": increasePerso("bonusPower", +$v); break; } } } /** * Generates an XML tree describing the Item * * @param SimpleXMLElement $xml root XML element to add the Item's property to * @param int $count numbers of Items * @return void */ function addToXML($root, $count) { $item = $root->addChild("item"); $item->addChild("name", $this->name); $item->addChild("cost", $this->cost); $item->addChild("icon", $this->icon); $item->addChild("desc", $this->desc); $xmlfeat = $item->addChild("feat"); foreach($this->feat as $k => $v) $xmlfeat->addChild($k, $v); $item->addChild("count", $count); } /** * Generates an Item object from an XML tree * * @param SimpleXMLElement $xml root XML element representing the Item * @return void */ public static function fromXML($xml) { $feats = array(); foreach($xml->feat[0] as $k => $v) $feats[(string)$k] = (string)$v; return new static((string)$xml->name, +(string)$xml->cost /* convert to number */, (string)$xml->icon, (string)$xml->desc, $feats); // Call constructor } } ?>