/    Sign up×
Community /Pin to ProfileBookmark

OOP design for a forum

Hi all

(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.

Coming from a JavaScript background (if you can call 2-3 years a background), i am use to making everything a function and keeping code clean by doing so. Eventual however, i still find this gets messy so i would like to get into OOP programming as soon as possible.

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.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 18.2014 — 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 [url=http://en.wikipedia.org/wiki/Composite_pattern]Collection Pattern[/url], 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?
Copy linkTweet thisAlerts:
@B_vidlerauthorAug 18.2014 — 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.
Copy linkTweet thisAlerts:
@NogDogAug 18.2014 — To continue the example of posts within a thread, you could have the Thread class [i]implement[/i] 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 .
×

Success!

Help @B_vidler spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...