Answer the question
In order to leave comments, you need to log in
OOP: Is it architecturally correct to do this?
Hey!
Working in some systems, I often have to make selections from various sources (nosql database, external api (or some internal modules), etc.) and most often, the result of such selections is an array of parameters and values.
In large projects, I find it difficult to work with these arrays because of the many problems with it.
Therefore, I wrap all this data in objects:
For example:
I have a method of the module for working with orders, which returns an order by its identifier.
An order is a set of parameters of the order itself, its buyer, goods, etc.
When designing, I get the following:
<?php
class Order {
private array $order_fields;
private Buyer $buyer;
private BasketItems $basket_items;
public function __construct(array $order_fields, Buyer $buyer, BasketItems $basket_items) {
$this->order_fields = $order_fields;
$this->buyer = $buyer;
$this->basket_items = $basket_items;
}
public function getId(): int { return $this->order_fields['order_id']; }
public function getAmount(): float { return $this->order_fields['order_amount']; }
public function getBuyer(): Buyer { return $this->buyer; }
public function getBasketItems(): BasketItems { return $this->basket_items; }
}
class Buyer {
private array $buyer_fields;
public function __construct(array $buyer_fields) {
$this->buyer_fields = $buyer_fields;
}
public function getId(): int { return $this->buyer_fields['id']; }
public function getFullName(): string { return $this->buyer_fields['full_name']; }
public function getPhone(): string { return $this->buyer_fields['phone']; }
}
// ...
Answer the question
In order to leave comments, you need to log in
Perhaps this is normal, as it should be? Or how is it architecturally correct to solve this?
As a result, at some level of the application, a selection occurs, and then all the necessary objects are created and embedded into each other. (By the way, what is the name of this application layer, and in whose area of responsibility is this task?)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question