www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP - OOP design/performance

  1. #1
    Join Date
    Oct 2012
    Posts
    12

    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
    Location
    Ankh-Morpork
    Posts
    18,925
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2012
    Posts
    12
    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
    Location
    Ankh-Morpork
    Posts
    18,925
    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:
    <?php

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

    class 
    User extends Model
    {
        protected 
    $userData;
        public function 
    read(Array $keys)
        {
            if(!isset(
    $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);
            
    $stmt->execute(
                array(
                    
    ':username' => $keys['username'],
                    
    ':password' => $hashedPassword
                
    )
            );
            
    $this->userData $stmt->fetch(PDO::FETCH_ASSOC);
            return (bool) 
    $this->userData;
        }
    }

    class 
    AdminUser extends User {
        public function 
    read(Array $keys)
        {
            
    parent::read($keys);
            if(!empty(
    $this->userData)) {
                if(
    $this->userData['admin'] == false) {
                    
    $this->userData null;
                }
            }
            return (bool) 
    $this->userData;
        }
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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



Recent Articles