Answer the question
In order to leave comments, you need to log in
Is it correct to pass objects (with private properties and getters) to a read-only view?
I made the architecture of my project in such a way that I wrote the classes under the entities from the database. And connections in a DB it is sometimes a composition in classes and so on. So, for example, instances of user classes can be stored in the Message class as an identification of who sent it to whom. Lots of DTO objects. I did it this way because I don't want to use Eloquent. I don't like that the object can store itself in the database, do some other garbage ($user->all() ). To do this, I have repositories: they get a string from the database, pass it to the class constructor, and already return an object that is responsible (encapsulates) there for both its state and behavior (or generate new ones). For me it is very convenient, and most importantly, it fits in my head. And generates fewer dependencies. entities on their own. It turns out that the controller can pull the service, the service pulls the repositories and receives entities (classes). Further, these entities somehow interact, the service sends them back to the repository for saving in the database.
Question: is it correct to pass such objects to view to read properties? They are naturally private (with getters), but I'm worried that, in theory, an object can change its state right in the view by calling some of its methods. I tried to implement the "toArray()" method for each class, but: 1) constant problems with types (repositories then return not objects but collect) 2) when an object, then PHPstorm helps with hints, which is convenient, and an array is hard to remember 3) in objects can be many levels of compositions
By the word correctly, I mean that in the future for a large and long project with a bunch of tables, will it ever come out sideways (it will be difficult to debug for example).
Here is an example of one such Message class constructor:
public function __construct($userFrom, $userTo, $message, $id = null)
{
$this->id = $id;
$this->userFrom = $userFrom;
$this->userTo = $userTo;
$this->message = (string)$message;
$this->created_at = now();
}
Answer the question
In order to leave comments, you need to log in
pulling database entities into the presentation layer is such a thing, change the field and all the templates using will be covered, and will be covered that you will not notice, for example, a notification will come to the client and his name will disappear there.
so I try to pass the desired array / DTO structure to the view
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question