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.

  • 1
    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?
    – Andy
    Commented Mar 5, 2016 at 12:58
  • As of right now, the class Category contains private variables along with getters and setters. It is pretty simple.
    – Dacramash
    Commented Mar 5, 2016 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. Commented Mar 6, 2016 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
    Commented Mar 7, 2016 at 13:08
  • This sounds like a basic data access problem. Do a little reading about data access objects and the repository design pattern. Commented Nov 22, 2018 at 15:06

2 Answers 2


You can always create multiple instances of a class! This is what classes are designed for! Each object will hold its own individual inner variables (unless they are static, in which case they are shared).

If you wish to store them all into a variable, you can use any collection type (List, Collection, Array). PHP offers its own array type

If you wish to pass a list of categories to a function, or return a list of categories. Fill the array with objects and pass it along.


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.

  • 2
    Why make a CategoryList instead of a List<Category>? This would add a class for no benefit.
    – TZubiri
    Commented Nov 22, 2018 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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