OOP design for a forum
(disclaimer: if i am speaking gibberish in the post then i apologies as Iím a little ill today haha)
So i am highly new to the concept of OOP code and am trying to create a few projects to get my head fully around it.
I know the usual - mammal - dog - barks concept.
In my current website that i am creating for an assignment i thought creating a forum would be a good idea so why not make it OOP?
My first idea was simple,
All forums have subjects (or sections)
All subjects have threads
All threads have posts
So a class would be made for each that extended from forum. This idea cannot work (for me at least) because:
If a forum class were to be put on the forum pages which linked to a forums database with that forums name (so a construct parameter "forum name") it would do a query to find all subjects and display then. However, if this then made a subject class for each result it would be making that many forums (due to the classes being extended). This problem would then keep going down the tree ending up with each post being a new instance of a forum ... umm .. yer.
Cleary that logic is flawed and therefore, so is my idea of how classes should be implemented.
After coming to the above conclusion i then removed to extended logic and made them separate classes. If you then think that a forum instance would create several subject instances and so forth this then begins to work but isn't this just making fancy functions? - if the classes don't actual link together.
So, i ask you, how would you go about doing this? Would you at all, or would this be a procedural piece of code?
side note: in terms of the database that would behind this, unless being oop changes this, I am competent enough to need no help with, so in any replies don't worry about going into that side of it.
Thanks for any replies/ideas.
The general rule of thumb for extending is if you can replace "extends" with "is a", and that makes sense. If we say "Post is a Thread", that does not really make sense, so I would agree with your assessment that you should not be extending it (with the caveat that you can always change your mind later). Instead, we're looking here at a case where "Thread contains one or more Post objects", so your Thread class would likely contain a property that is an array of Thread objects, or possibly just a ThreadCollection object that, perhaps, implements the Collection Pattern, which might be useful if you want/need to have a hierarchical structure to posts, replies to posts, replies to replies, and so on. (If not, I'd keep it simple and just have that array of Post objects.)
Does that help any?
Yes your generaliation helped a lot, so thank you there. I have also just been readying up on php classes and (as dumb as it now sounds) did not realise you could make classes in classes. Which would fit into what you said about making an array of objects (while still keeping it clean).
Thanks for the quick reply.
To continue the example of posts within a thread, you could have the Thread class implement Iterator, and design it so that is you iterate on a Thread instance (e.g. via foreach()), it actually loops on that array of Post objects. See Example #2 at http://php.net/iterator .
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)