#1
  1. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,971
    Rep Power
    375

    Calling 1 class method or several?


    If i have a class something like:

    class {

    function login
    function add_data
    function logout

    }

    Now i could call each functions individually OR have another method

    function __construct($all_needed_data){
    //call three methords above
    }

    so which is preferred option?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    Whatever makes the most sense to you. Do those three methods have to be called every time the object is created? Then make it easier for everyone and call them in the constructor. If not then don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,971
    Rep Power
    375
    thanks. It makes more sense for me to have them in one method but i thought maybe it is bad practice
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    It really depends what they do and how they're supposed to be used.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2
    It depends on how reusable and loosly-coupled you want these methods to be. Lets say these methods can be reused in another program and objects, and not all of them have to be called at the same time, it will be a better choice to create three public methods separately. This way the other program/object does not have to call all of them at the same time, a good example would be an update method that saves user data. Sometimes you want all of the fields to be updated at once, sometimes you only want one or two to be saved.

    If they are only to be used in one or two specific circumstances and must be called simultaneously or even sequentially, wrapping them up in a constructor or one particular method may sound just about right. This is usually the case for utility class/objects that handle the startup/bootstrap of your program, since you'd like all of the necessary modules in your script to load and function.
    Last edited by Hall of Famer; March 9th, 2013 at 04:02 PM.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    2
    Well, this doesn't make much sense. You have a "login" and "logout" method. I can't think of any reason why you'd need to call both methods at the same time.

    So unless you have an appropriate reason for doing such, you should call them separately.

    But then again, that's me assuming those "login" and "logout" methods are for some sort of member login system. I could be completely wrong.

    If that is the case, then have a think about how you're planning to use this class. Are you SURE you'll never have to call just one of the methods? If you think there's a possibility of you having to call only one (or two) of them, then don't have them in the constructor.
  12. #7
  13. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    I wouldn't have the add_data method in there

    lets act on a dew assumptions and expand your code a little:

    PHP Code:
    class user_user {

        public 
    function__construct() {
            
    //any code common to any of the methods
            //or any 'setup' required
        
    }

        public function 
    login () {
        
        }
        
        public function 
    logout() {
        
        }

    }


    class 
    user_model {

        public 
    function__construct() {
            
    //any code common to any of the methods
            //or any 'setup' required
        
    }
        
        public 
    fuction add_data($data) {
        
        }

    }

    //if login requested

    $user =  new User;

    $user->login();


    //if logout requested

    $user =  new User;

    $user->login();


    //if add data

    $um = new user_model;

    $um->add_data($data); 
    you can, of course, combine any of these examples with your own logic: For example checking if a user is logged in or not, attempting a login and then, if successful, adding data.

    E-Oreo's thread, how to program a basic but secure login system, may provide a useful insight
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,971
    Rep Power
    375
    thanks guys for elaborating and explaining.

    These three methods will almost ALWAYS need to be used at once.. one data url_encodes the post, the other method creates a stream context, the third method does file_get_content so they will always need to be called one after the other..
  16. #9
  17. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Have you considered making one method which does everything? or do you feel that each part could be used independently?


    Consider this: ABSTRACTION FOR THE SAKE OF ABSTRACTION? WHY ADD UNNEEDED COMPLEXITY?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo