lastid=0; $this->data=array(); } public function update($params){ foreach ($this->data as $item){ if ($item->getId()==$params['id']){ $item->update($params); break; } } } public function display(){ foreach ($this->data as $item){ $item->displayInfo(); } } public function delete($id){ for ($i=0;$idata);$i++){ if ($this->data[$i]->getId()==$id){ array_splice($this->data, $i, 1); break; } } } public abstract function add($params); } class CategoryList extends BaseList{ public function add($params){ $this->lastid++; $params['id']=$this->lastid; $category=new Category($params); array_push($this->data,$category); } } class ParameterList extends BaseList{ public function add($params){ $this->lastid++; $params['id']=$this->lastid; $category=new Parameter($params); array_push($this->data,$category); } } class BackpackList extends BaseList{ public function add($params){ $this->lastid++; $params['id']=$this->lastid; $category=new Backpack($params); array_push($this->data,$category); } } class Category{ private $id; private $name; public function __construct($params){ $this->id=$params['id']; $this->name=$params['name']; } public function __delete(){} public function update($params){ if (isset($params['name'])){ $this->name=$params['name']; } } public function displayInfo(){ echo $this->id.'. '.$this->name.'
'; } public function getId(){ return $this->id; } } class Parameter{ private $id; private $name; private $unit; public function __construct($params){ $this->id=$params['id']; $this->name=$params['name']; $this->unit=$params['unit']; } public function __delete(){} public function update($params){ if (isset($params['name'])){ $this->name=$params['name']; } if (isset($params['unit'])){ $this->unit=$params['unit']; } } public function displayInfo(){ echo $this->id.'. '.$this->name.' ('.$this->unit.')
'; } public function getId(){ return $this->id; } } class Backpack{ private $id; private $brand; private $model; private $price; private $category; private $parameters; public function __construct($params){ $this->id=$params['id']; $this->brand=$params['brand']; $this->model=$params['model']; $this->price=$params['price']; $this->category=$params['category']; $this->parameters=$params['parameters']; } public function __delete(){} public function update($params){ if (isset($params['brand'])){ $this->brand=$params['brand']; } if (isset($params['model'])){ $this->model=$params['model']; } if (isset($params['price'])){ $this->price=$params['price']; } if (isset($params['category'])){ $this->category=$params['category']; } if (isset($params['parameters'])){ $this->parameters=$params['parameters']; } } public function displayInfo(){ //echo $this->id.'. '.$this->name.' ('.$this->unit.')
'; echo ''.$this->id.'. '.$this->brand.' '.$this->model.'
Ціна: '.$this->price.' грн
Категорія: '.$this->category.'
Характеристики:
'; $param_data=json_decode($this->parameters,true); foreach ($param_data as $item){ echo $item["name"].': '.$item["value"].'
'; } } public function getId(){ return $this->id; } } $data1=array('name'=>'Міські Рюкзаки'); $data2=array('name'=>'Трекінгові Рюкзаки'); $data3=array('id'=>'1','name'=>'Експедиційні Рюкзаки'); $catList=new CategoryList(); $catList->add($data1); $catList->add($data2); $catList->update($data3); //$catList->add($data3); //$catList->delete(2); $catList->display(); //$data=array('id'=>1,'brand'=>'Osprey','model'=>'Flare','price'=>'3000','category'=>'Міські рюкзаки','parameters'=>'[{"name":"Вага","value":"1 кг"},{"name":"Об\'єм","value":"23 л"}]'); //$data2=array('id'=>2,'brand'=>'Fjord Nansen','model'=>'Bodo','price'=>'4000','category'=>'Туристичні рюкзаки','parameters'=>'[{"name":"Вага","value":"1.6 кг"},{"name":"Об\'єм","value":"40 л"}]'); //$a=new Backpack($data); //$a->displayInfo(); //$a->update($data2); //$a->displayInfo(); ?>