Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a class named "Category" to handle all the operations and data about my categories. Now, my class is created however I need to find a way to build an object for each of the categories in my database.

In a PHP OOP structure, how should I do this? I was thinking about maybe making some kind of CategoryCollection class which only purpose would be to create an array containing a Category object for each of my categories, however I am not quite sure this would be the best way to achieve this.

share|improve this question
    
Something does not add up about your design. You're saying you have a class Category (singular) which handles all the operations of all your categories (plural). It seems like a design failure. What exactly is the Category class as of now? – David Packer Mar 5 at 12:58
    
As of right now, the class Category contains private variables along with getters and setters. It is pretty simple. – Dacramash Mar 5 at 19:41
    
It sounds more like a data entity than a class. Which is fine, but its useful to realise that there's a conceptual difference between an entity, which is usually synonymous with 'data model', versus a class, where the emphasis is on behaviour. This distinction doesn't directly affect your code, but it might affect the way you think about your design. When you start adding behaviour, it would probably be a good idea to break it up into pieces, so that you avoid creating a bloated "do everything" class. – Ben Cottrell Mar 6 at 8:25
    
Thanks for your comment. I see what you mean by the entity versus class, however I am not so sure about how to break it up into pieces... Right now, my classes are exactly like you said, they do pretty much everything. I use my "controllers" to do simple operations such as creating an instance of a class and calling its functions. – Dacramash Mar 7 at 13:08

Typically for creating multiple instances of the same class you would use a Factory design pattern. This would put the responsibility of creating the objects with the factory. You can then have a container, like an array, store the objects.

share|improve this answer
    
So then my Factory would have a function like "buildAllCategories" which would call a Category constructor for all my categories? And the php code would look like $myCategories = CategoriesFactory::buildAllCategories(); ? – Dacramash Mar 5 at 19:44
    
Yes, this is one way of doing it. If the constructor itself takes a large number of parameters or there are additional steps in creating a single instance, you could also have a createCategory method which is invoked by the buildAllCategories method. – Miguel van de Laar Mar 7 at 13:42

It sounds like you need two classes:

  1. Category, which stores data about a single category. This is likely to be a simple POJO, typically holding data from a single database row. There may be some behavoural mehthods, but often there are none.
  2. CategoryList, which contains a list of categories, or possibly extends somethig like ArrayList. Typically this will supply only behavoural methods like Category findCategoryByCode(String categoryCode)and List<Category> findIncomeCategories().

It seems like your existing class is trying to fill both functions - try splitting out the responsibilities. Apologies that my answer is Java oriented. I have never used objects in PHP, but the same basics should apply.

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.