PHP OO - how to initialize your business objects?

By business model or business objects, I mean simple old objects, such as "User" with all their property names, address, ...; in addition to all user properties, it can be said that each user will have an “AppointmentBook” object, each book has a set of “TimeSlot” objects, etc. There are objects in the business model with links between them, at least the way I code the business model in Java. The question arises:

To initialize my business objects, in Java , I would

  • retrieve all data from the database only once during application initialization,
  • map data from my database to my business objects.
  • save in memory (cards) and they will be available for all requests.

PHP Share-Nothing-Architecture confuses me for proper OO programming: If I use the same logic, I will have to extract all the objects from the database for each request (I know that I can still cache, but you don’t cache all of my DB, this is not a question about caching, but rather about the way of programming in PHP and its architecture).

So, let's say that for one HTTP request, I just need the User properties, and I do not need to access its destination book. It would be useless to retrieve all the data from the database for all the objects that the user refers to, since I just need its properties. This means that I will initialize PHP objects from my model with a large number of NULL values ​​(NULL due to objects contained in User, which I will not load), which can subsequently lead to errors.

I was wondering how professional PHP developers typically use their business objects. (I come with Java)


UPDATE: It was foolish to say that I would load the entire database into memory while running the application in Java. What I prefer is that if I need to get a specific user, I can just download all of his data and this will be available through all the requests.

+6
source share
3 answers

In PHP, you do not store all the business model data of your domain in memory. Instead, you only request the database (although the cache, if necessary), the data you need.

The model level in php should be built from several domain objects and data (I assume that this part is not so different from Java). If you need User details, you only get this information from the database / cache. You will most likely have a separate calculator, intended only for communication with the user.

You display information about this user and forget about the request. The next query (when and when it appears) will require different information. You might need a ContactList for this User ... then you really don't need the user himself, only his user_id . Again, you make it possible to map data to the domain object responsible for processing the contact list, and if the contact list contains User instances, just create them, but leave them in the “non-started” state (the object knows only its own user_id). Only select them if you really need to, and only those parts that you will use are that “look”.


PS you may receive notifications, I said that the model should be segmented later, but quite often php developers simply create a separate class for each DB table (which implements ActiveRecord) and call it a “model”. This is the result of the impact of Ruby on Rails on PHP frame developers, which, IMHO, is one of the worst things that happened to PHP in the last 5 years.

+2
source

In your Java example, it means storing the entire contents of the database in memory. If you do this, then what is the point of the database? Why not just create all these objects and memdump them for saving.

If I use the same logic, I will need to extract all the objects from the database for each query

It's just crazy, you don’t need to take anything, you create new instances when you need them, and destroy them when you no longer need them.

So, let's say that for one HTTP request, I just need the User properties, and I do not need to access its destination book.

It's easy, reverse engineer your user. The user needs his properties and a property called assignBook, which is just a collection of business card identifiers.

If you really need these meetings, you can get them from the database later.

This means that I will initialize PHP objects from my model with a large number of NULL values ​​(NULL due to objects contained in User, which I will not load), which can subsequently lead to errors.

Not really, if so, your User object is too large. Reduce it, you must download the entire user. In addition, the user must be small enough so that you can intelligently download it.

If you do not want this, you can always create a UserProperties class and let each user have one. When you load a user, you load properties, but you also have the option of creating properties separately.

+2
source

Even in Java, you will not load all the data from the database into memory. However, you, as you write, often download more than the short Transaction Scripts that PHP typically has.

Models should be “smart” and then only load data from the persistence store necessary to complete the requested action. This requires the object to be smart enough for lazy loading of data.

This can be achieved with the help of the Domain Model , which knows quite well about itself and the Data Mapper , which knows enough about the repository.

There are other templates that can meet your needs depending on the type of application, however, the domain model with the Data Mapper is quite flexible.

An example data converter in the world of PHP Doctrine .

+1
source

Source: https://habr.com/ru/post/902485/


All Articles