Results 1 to 4 of 4

Thread: OOP design for a forum

  1. #1
    Join Date
    Jul 2013

    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.

  2. #2
    Join Date
    Aug 2004
    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?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Jul 2013
    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.

  4. #4
    Join Date
    Aug 2004
    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 .
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11403 seconds
  • Memory Usage 2,864KB
  • Queries Executed 15 (?)
More Information
Template Usage (32):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates