M
M
Michael2015-05-04 13:58:55
PHP
Michael, 2015-05-04 13:58:55

How to access object A containing object B from object B?

I am learning PHP and OOP. As a training, I chose the task of creating a simple service similar to an online store.
In the procedural style, it was possible to implement the main functionality. I decided to translate it into the OO style, to consolidate what I read from books. Faced the problem of transition from associative arrays to objects.
There is an array containing user orders and information about their payment of the following structure:

[x] – заказ
  [user] – информация о покупателе
  [pays] - платежи
    [x] – конкретная оплата и сведения о ней
  [orders] - товары заказанные покупателем
    [x] – товар и сведения о нём

With the help of arrays, I was able to get all the data about the order by directly accessing specific array fields. For example, to add information about a specific payment to the database, I also need information about the buyer - his ID.
I decided to convert this structure into objects. Replaced orders with a Lot object containing a User object and arrays of Pay and Orders objects. Thus:
foreach ($purchase as $lot) {
  $lots[] = new Lot($lot);
}

where, lot:
class Lot {
  /**@var array Массив объектов с товарами */
  private $orders = array();
  /** @var array Массив объектов с платежами */
  private $pays = array();
  /** @var User Данные о покупателе */
  private $user;

  function __construct (array $lot) {
    $this->user = new User ($lot['user']);
    // Список платежей
    if (!empty($lot['pays'])) {
      foreach ($lot['pays'] as $pay) {
        $payObj = new Pay($pay);
        $this->pays[] = $payObj;
      }
    }
    // Список товаров
    if (!empty($lot['orders'])) {
      foreach ($lot['orders'] as $order) {
        $orderObj = new Order($order);
        $this->orders[] = $orderObj;
      }
    }
  }
}

I understand that most likely I did not design everything correctly. Therefore, I ask for any help.
1. How to correctly design a class that would describe the structure contained in the above array?
2. Maybe there is some kind of design pattern for solving this problem?
3. I can't figure out how to get access from a specific Pay object to the User object in order to get the user ID who made this payment?
4. There is an idea to pass a reference to the Lot object (self) to the Pay object when creating. How correct is this?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
matperez, 2015-05-04
@dm84ya08

1. It seems to me that it makes no sense to separate user payments into a separate entity and store them in the user model, because they do not exist without orders. It is better to separate them into order attributes (for example: when paid, how, payment transaction id).
3. Based on the first point, access to the user can be obtained through the order. Those. first, by the payment (transaction) id, you get an order, and from the order you get a link to the user who made this order.
What interests you is most likely described in such a thing as domain-oriented programming habrahabr.ru/post/61524/. It is also worth reading about relational databases and their normal forms habrahabr.ru/post/129195/.

V
Valentine, 2015-05-04
@Tpona

In my opinion, you need to first determine the location of data storage.
Read about the MVC Application Design Pattern and choose to learn an MVC framework such as yiiframework.com .
Because then you will come to this anyway, but the path will be long))

M
Maxim Kuznetsov, 2015-05-04
@max-kuznetsov

Everything is simple. You make a class for object A. Then - a class for object B. In class B, declare field a of type A. In class A, field b of type B. Initially, when creating an instance of class A, the reference to an object of class B in field b will be empty (null) . When creating an instance of class B, its field a with a reference to an object of class A will also contain the empty value null. Let the Link method be set for object A, which as a parameter receives a reference to an object of class B. Inside this method, you can assign the specified reference to object B to the field Ab, and then you can assign a link to object A, whose Link method was called .
Object B can be created in the constructor of object A, then set the values ​​to the fields in the same place.
Similarly, in class A, you can set a field of type an array of objects B. When you add a new object to the array, you can set a reference to A in field B.a.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question