Results 1 to 4 of 4

Thread: PHP - OOP design/performance

  1. #1
    Join Date
    Oct 2012

    PHP - OOP design/performance

    Hi all,

    Today I was thinking a lot about OOP and how to properly design a CMS with it. I found myself attempting to write out some classes, and I thought perhaps I would have a Member and Admin class to extend there super class User, but then I realized I would be creating objects for all these people. Would this not create a performance issue of some sort as these users' info will be stored in my database anyhow? I just thought it be nice to have it organized into classes instead of having these users being brought into a model to get them into functions of the controller. I find the code looks messy, but I would really like to have some input.

    By the way I am trying to write my own CMS using an MVC pattern. I know there are plenty of CMS' out there, but I am doing it to get more familiar with php oop and mvc. I have a bit of background in Java and therefore feel comfortable doing OOP, but not really with the concepts involved in creating websites. So please bare with me...

    Thanks in advance for any input, and excuse me if this is to general for you

  2. #2
    Join Date
    Aug 2004
    Well, your objects will only persist for as long as the invoking script is running -- those user objects will not be floating around anywhere in RAM or on disk once the script is finished and output has been sent to the browser; or am I misunderstanding your concern? (There are ways to persist objects across multiple requests, often controlled by sessions, but I would not start out a project with that in mind, myself, unless/until you find a need to do so.)

    Out of curiosity, are you building this on top of some PHP MVC framework (somewhat analogous to using Java Struts), or are you trying to do everything yourself?

    In any case, your idea of a (abstract?) User class extended by MemberUser and AdminUser classes sounds like good, basic OOD to me. In fact, User would probably extend a Model class, most likely, so you'd really have 3 levels of hierarchy.
    "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
    Oct 2012
    perhaps I am worried about nothing, and I think what you are saying about the execution does make sense. I just figured if I have these objects dynamically constructed say with a foreach loop run over an array which was passed to a function in the controller for example or wherever it may be needed. I'll keep your advice on not maintaining it over several requests in mind. Good to know.

    As far as the framework goes, I start off with codeigniter cause it is apparently easiest to get started with. However, I put it down again once I played around and notice I had a problem understanding some of the concepts such as sessions. So I went on to read up in the php manual and some peoples explanations on how it works and if I should use cookies or the database. My knowledge on how the web works I guess is not where it should be as of yet, but I figure if I have to build on php from scratch and figure out how to work with the web I should be able to work with a framework much more easily after. It is not for production so time is not a factor. I just want to really get as good as I can with oo php.

    Also, I was not using an abstract class, but perhaps I should be. I will also think about how I can extend the model and still make sense for the design. Could you perhaps elaborate on how you were thinking and why you think it should extend the model?

  4. #4
    Join Date
    Aug 2004
    If we assume for now that models in general will be objects that read/write data from/to a data source, it's reasonable to expect that any classes which basically represent things stored in that data source might share a lot of attributes and methods. Therefore, why not make one class that provides the "standard" parts of any models, which the more specific classes can extend and re-use?

    It may get a bit fuzzy determining how much specific stuff can actually be coded in the model class, and how much you might want to make abstract -- essentially making those aspects of the Model class more of an interface. (Which, of course, brings up the whole issue of interfaces, which I probably don't use enough -- and then there are always "traits", a new feature of the latest PHP releases that let's you sort of emulate multiple inheritance, but let's not go there now. )

    At a minimum, a model class might include properties/methods that handle connection to the database, logging of database errors, and then perhaps abstract methods for CRUD operations (create, read, update, delete). Then your User class would extend that, perhaps adding new methods specific to user objects, and then your classes for specific user types would add/modify things unique to those special types.

    Here's a simplified example just dealing with a read() method (and is by no means the only or even best way to do this, just some ideas to chew on):
    PHP Code:

    class Model
        public function 
    __construct(PDO $pdo)
    $this->pdo $pdo;
        abstract public function 
    read(Array $keys);

    User extends Model
        public function 
    read(Array $keys)
    $keys['username']) or !isset($keys['password'])) {
                throw new 
    Exception("Invalid keys");
    $hashedPassword sha1($keys['password']);
    $sql "SELECT * FROM users WHERE username=:username and password=:password";
    $stmt $this->pdo->prepare($sql);
    ':username' => $keys['username'],
    ':password' => $hashedPassword
    $this->userData $stmt->fetch(PDO::FETCH_ASSOC);
            return (bool) 

    AdminUser extends User {
        public function 
    read(Array $keys)
    $this->userData)) {
    $this->userData['admin'] == false) {
    $this->userData null;
            return (bool) 
    "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)

Tags for this Thread

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.16210 seconds
  • Memory Usage 2,903KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (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)bbcode_php
  • (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 (71):
  • 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_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
  • 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