How to implement correctly?

class Order{ public $id; public $ProductList; } class ProductList{ public $Order; public $Product; public $quantity; } class Product{ public $id; public $name; public $Category; public $Price; } class Category{ public $id; public $name; public $noAmount; } class Price{ public $id; public $priceWithDiscount; public $priceWithoutDiscount; } 

For example, I need to get an array of orders Order. How to implement the filling of all nested objects? All objects are stored in separate database tables, linked by ID. Write a single request and create all objects during a crawl?

  • 2
    Eh. If I were you, I would think a little more about the structure. After all, creating a class under the list of products and separately under the product is meaningless somehow. After all, the list of products is a product, but not one. And since there are different (for some reason) fields, I would recommend you to read about design patterns and interfaces. - Alexey Shimansky
  • What, productList will have its own methods that are not applicable to one product. And yet, if I need to fill out just such a structure? - Artem Kolos
  • wrong structure - Alexey Shimansky
  • @ Alexey Shimansky, I suspect, the author wanted to get a bit more reinforced feedback than the "wrong structure" - etki
  • Still, how will it be right? Write a single query and create all objects during a crawl or in the object's constructor fill in the embedded objects during creation? - Artem Kolos

2 answers 2

It seems to me that your structure is really not very correct. At a minimum, the ProductList and Price classes can be discarded. I would suggest the following structure:

 class Product { public $id; public $name; /** * * @var Category */ public $category; public $priceWithDiscount; public $priceWithoutDiscount; } class Category { public $id; public $name; public $noAmount; } class Order{ public $id; /** * Массив объектов Product * @var array() */ public $productList; } 

Well, I received an array of Order objects by some parameters.

     class Product { public $id; public $name; public $category; public $discount; public $price; public function getRealPrice() { return $this->discount ? $this->discount->calculate($this->realPrice) : $this->realPrice; } } 

    Those. There is a separate discount object that we can assign at any time. Depending on it, the real price will vary.

     class Category { public $id; public $name; public $noAmount; } 

    There is no change.

     class Order implements Countable, Iterator { public $id; /** * Массив объектов Product * @var array() */ $productList; public function getTotalPrice() { // подсчет общей стоимости } /* Реализация интерфейсов */ } 

    Here is a list of products as in the answer @ S.Pronin, but you need to make this object model iterable and Countable (if possible), then you can write something like count($order) , or foreach($order as $product)

    And finally, a discount:

     class Discount { public $id; public $discount; public function calculate($price) { return $price - $price * $this->discount; //например, скидка 15%, тогда $discount должен быть 0.15 } } 

    Such a discount model is suitable, for example, for discounts of the type “Disposal - all by 50% each”, then one object is assigned for all goods, and it is possible to simply change M% to N%. Of course, if each product has its own unique discount, this method does not quite fit.