PHP OO - how to initialize your business objects?
Asked Answered
S

3

6

By business model, or business objects, I mean plain old objects like a "User" with all their properties name, adress, ...; in addition to all the user properties let's say each user would have an "AppointmentBook" object, each book has a set of "TimeSlot" objects, etc. The business model has objects with references between them, at least that's how I code a business model in Java. Here comes the question:

To intialize my business objects, in Java, I would

  1. fetch all of the data from DB only once during application initialization,
  2. map data from my DB to my business objects
  3. store in memory (maps) and they would be shared across all the requests.

PHP's Share-Nothing-Architecture is confusing me for proper OO programming: If I use the same logic, I would have to fetch all the objects from DB, for every request (I know I could still cache, but you don't cache all of your DB, it's 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 don't need to access his appointment book. It would be a pitty to fetch all the data from the DB for all the objects the User makes reference to, as I just need his properties. This means that I will initialize PHP objects from my model with a lot of NULL values (NULL because of the objects contained in User that I won't load) which can later on lead to errors.

I was wondering how professional PHP developers usually use their business objects? (I'm coming from Java)


UPDATE: It was kind of stupid to say that I would load the whole database into memory during application init in Java. What I rather meant is that, if I need to fetch a specific user, I could just load all of its data and that would be accessible through all the requests.

Swarth answered 27/11, 2011 at 11:23 Comment(1)
it seems to me that OP is more familiar with desktop application development .. desktop devs _can_ load large chunks of information in the memory and just keep them.Lifeanddeath
H
2

In PHP you do not keep all the data of your domain business model in the memory. Instead you only request from DB ( though cache, if needed ), the data you want.

Model layer in php should be built from multiple domain object and data mappers ( i assume, that part is not so different from Java ). If you need User details, then you fetch only that information from database/cache. You most likely will have a separate mapper just for dealing with user(s).

You display the information about that user, and forget about the query. Next request (when and if it comes) will require different information. Maybe you will want ContactList for that User ... then you really do not need user itself, only his user_id. Again, you let you mapper to fetch data into the domain object responsible for handling contact list, and if contact list contains User instances, then just create them, but leave in "unfetched" state (object knows only own user_id). Fetch them only if you really need to, and only the parts which you will use ins that "view".


P.S. you might have notices, I told that model later should be segmented, but quite often php developers just create single class of each DB table (which implements ActiveRecord) and call it "model". This is a result caused by Ruby on Rails influence on php framework developers, which, IMHO, is one of the worst things that has happened to PHP in past 5 years.

Herewith answered 27/11, 2011 at 13:1 Comment(0)
O
2

Your Java example implies your storing your entire databases content in memory. If your doing that, what's the point of the database? Why not just create all those object and memdump them for persistence.

If I use the same logic, I would have to fetch all the objects from DB, for every request

That's simply madness, you don't need to fetch 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 don't need to access his appointment book.

That's easy, redesign your user. Your user needs it's properties and a property called appointmentBook which is simply an array of appointment book ids.

If you actually need those appointments you can fetch them from the database later.

This means that I will initialize PHP objects from my model with a lot of NULL values (NULL because of the objects contained in User that I won't load) which can later on lead to errors.

Not really, if this is the case your User object is too big. Make it smaller, you should load the entire user. Except of course the user has to be small enough for you to sensible load it.

If you don't want that then you can always create a UserProperties class and let every User have one. When you load the User you load the properties, but you also have an option to create the properties seperately.

Oosphere answered 27/11, 2011 at 13:53 Comment(0)
V
1

Even in Java you would not load all data from the database into memory. You can however - as you write - often load more compared to short Transaction Scripts you normally have in PHP.

You models should be "clever" then to only load the data from the persistence storage that is needed to perform the requested action. This requires the object to be "clever" enough to lazy-load data probably.

This can be achieved with a Domain Model that knows enough about itself and a Data Mapper that knows enough about the storage for example.

There are other patterns as well which might suit your needs depending on the type of application, however a Domain Model together with Data Mapper is quite flexible.

An exemplary data mapper in the PHP world is Doctrine.

Vey answered 27/11, 2011 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.