Answer the question
In order to leave comments, you need to log in
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] – товар и сведения о нём
foreach ($purchase as $lot) {
$lots[] = new Lot($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;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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/.
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))
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 questionAsk a Question
731 491 924 answers to any question