Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to get my head around object oriented programming as it pertains to web development (more specifically PHP).

I understand inheritance and abstraction etc, and know all the "buzz-words" like encapsulation and single purpose and why I should be doing all this.

But my knowledge is falling short with actually creating objects that relate to the data I have in my database, creating a single object that a representative of a single entity makes sense, but what are the best practises when creating 100, 1,000 or 10,000 objects of the same type.

for instance, when trying to display a list of the items, ideally I would like to be consistent with the objects I use, but where exactly should I run the query/get the data to populate the object(s) as running 10,000 queries seems wasteful.

As an example, say I have a database of cats, and I want a list of all black cats, do I need to set up a FactoryObject which grabs the data needed for each cat from my database, then passes that data into each individual CatObject and returns the results in a array/object - or should I pass each CatObject it's identifier and let it populate itself in a separate query.

share|improve this question
2  
Read some stuff about ORM (Object Relational Mapping). What you're refering to is a very common problem. –  K.. Jul 3 '13 at 11:09

2 Answers 2

up vote 1 down vote accepted

Creating 100, 1 000 or 10 000 similar objects is not itself an issue (unless the object itself is huge). If you find yourself creating thousands and thousands of objects, ask yourself if you really need to.

If you're loading Cat objects, one per cat stored in the database, do you need to load all the 500 000 cats, for example? Why? You can't display them on the screen: it wouldn't be particularly useful. If you need to process them, is preloading everything the best way to do?

When you need a list of black cats, you don't load every cat available. What you do is to limit, at database level (for example with a where [color] = @color), the results you get, and you build objects corresponding to black cats only.

Indeed, this can be done in a factory which will contain a method which takes a specific color and returns the cats of this color.

Doing a query per cat, on the other hand, is a terrible approach. The fewer queries you do, the better. That's why your suggestion to use a factory was a good idea.

share|improve this answer

What you need is Object Relational Mapping. You have mentioned that you already have a class Cat that creates objects which represent one row in the database table. What about a class CatList that represents a list of Cats?

Here is a short example how it conceptually can work:

$catList = new CatList();
$catList->readObjects();
$cats = $catList->getObjects();

In the readObjects method you then make a query that loads either all cats (SELECT * FROM cat) or cats with certain limitations (SELECT * FROM cat WHERE xy = ? or SELECT * FROM cat LIMIT 30).

For each of the read rows you create a Cat object. But in the constructor you have the opportunity to either give an ID (and let the Cat load the data) or the data (and skip the load step).

That way you just have one query for all the Cat objects you need. If you wanted a list of black cats only, you could do the following:

$catList = new CatList();
$catList->addCondition('color = ?', 'black');
$catList->readObjects();
$cats = $catList->getObjects();

Inside the CatList you generate the query dynamically depending on possible conditions. As you won't change the list of cats very often, you could then cache the resulting array $cats in a file via serialization. Upon need you would simply need to unserialize the array and then use the stored Cat objects. If you change something to the cat table, you recreate the cache.

Result: Problem solved.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.