SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Applying OOP to small open source project?

    So my current understanding of classes are:
    Singleton for a class that will only ever be instantiated once.
    Static for a class that doesn't get instantiated but just exists.
    Regular? For a class that can get instantiated over and over.

    So I'm doing a small open source project and as for dealing with users, I thought of how I could deal with it, for example:
    Creating a user - I could instantiate a users object and then call a method create on it. Or I could have a singleton so the users object always exists and call create on that?

    I just think it seems sort of sloppy to create an object for each user related action, like updating a users credentials, would I want to instantiate another user object and then call a method update on it?

    Just confused about how to actually apply OOP, and the best way to do.

    Thanks for any/all help you guys can provide.

  2. #2
    SitePoint Enthusiast praveenkv1988's Avatar
    Join Date
    May 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should make sure the necessary objects are created in constructors. Then you can call the appropriate methods to complete the actions.

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To be honest, I would suggest reading several books on OOD/OOA/OOP before continuing forward.

    Cheers,
    Alex
    The only constant in software is change itself

  4. #4
    SitePoint Member
    Join Date
    Nov 2007
    Location
    North East England
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Are you using a particular framework for this such as CodeIgniter or Zend?

    I would recommend using some sort of MVC framework for this project . Within this I would then define a user controller and model.

    A singleton may work for the user object have a look at this article for some more details on developing a good singleton in PHP.

    Thanks
    nathj07
    Keep on trying, keep on learning

  5. #5
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think I've ever actually created a singleton personally. I know that they've had their uses historically, but I've never actually touched one. I have built some static classes but they're rare, and unless you really understand threading, you can't guarantee your singletons and/or static classes are truly unique. This is only becoming more and more of a problem as multi-core PCs and servers proliferate.

    Now, me myself personally, this is how I would probably think of this. If you create a static class with a CreateUser() function, then that doesn't seem like a User class. That's a class that creates User classes, so it's probably a UserManager/UserFactory class. That's one pattern I've used. Having a class to manage object creation is a good choice when the creation is a complex issue (e.g. factory classes encapsulate object creation when you change the object a lot, and / or you have many versions of an object that you have to choose between. a factory would only contain code for object creation though, if you create an object and do other things with it as well, then it's not really a factory, it's a more general purpose manager)

    The alternative, like praveenkv1988, is to write whatever code you need to create a user in the constructor logic. It doesn't make sense to me to instantiate a user object and then call a Create() function against it. If you asked me to explain instantiation to a non-programmer, I would probably explain it as "creating an object". Therefore, any creation code should be written into the constructor because that's the function which is automatically run when the object is instantiated. This is a good choice when a manager or factory class just seems like overkill.

    Once your User is created, then absolutely, you can start calling methods against it. In your example, if you change the data in it, it makes total sense to have an Update() function to run which saves the changes back to the database. But you can also create a pair of users and compare them to see if they're the same (that would be one way to approach authentication), or whatever else you want to do.
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  6. #6
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cant you just do this:

    PHP Code:
    $user = new User();
    $user->create('mark');
    $user->create('ben');
    $user->create('sam');
    $user->delete('mark');

    echo 
    $user->get('ben');

    class 
    User 
    {
        private 
    $_user = array();
        
        public function 
    __construct()
        {
            
    $this->db Registry::get('database');
        }
        
        public function 
    create($name)
        {
            
    $this->db->query('create user....');
            
    $this->_user[$username] = 'i dont know';
        }
        
        public function 
    delete($name)
        {
            
    $this->db->query('delete user...');
            unset(
    $this->_user[$username]);
        }
        
        public function 
    get($name)
        {
            return 
    $this->_user[$name];

            
    // or if you wanted to get their db creds...
            //this->db->query("select * from users where name = '$name'");
        
    }
        



Bookmarks

Posting Permissions

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