tags:

views:

139

answers:

5

In OOP, is it better to use class attributes within class functions, or just pass parameters to them.

class User{
  private $user = array();

  public function Get_Existing_User($user_id){
    //SQL selects user info for existing user
    $this->user = mysqli_fetch_assoc();
  }

  public function Set_User($user_data){
    $this->user = (array) $user_data;
  }

  public function Add_User(){
    //insert everything from $this->user into database
  }

  public function Get_User(){
    return $this->user;
  }
}

VS

class User{

  public function Get_Existing_User($user_id){
    //SQL selects user info for existing user
    $user = mysqli_fetch_assoc();
    return $user;
  }

  public function Add_User($user_data){
    //insert everything from $user_data into database
  }
}

Whats the better way to go?

A: 

Class attributes are expected to describe the state of an instance of the class known as an object. As such, the attributes can be used by any function of the class to modify it's state. Function parameters on the other hand may have nothing to do with the current state of the object but can be used to modify it's state.

For example: a user object could be expected to have a user name attribute, a password attribute, and an authenticated attribute. this user object also has a function called authenticate that takes a parameter which describes an authentication method. The parameter is used to modify the state of the user object but would not be held as an attribute of it.

Joe Caffeine
A: 

That entirely depends on wether you're going to re-use the data and how you're using the Class.

If you create many individual instances of the Class and each Object represents a unique user, it makes sense to persist the data in a member variable. If you're using the Class as a DAO (data access object) with a lot of one-off operations, it probably doesn't make a lot of sense to persist the data. But even in a DAO, depending on its inner workings, it might make sense to store the data at least temporarily in a member variable if there are many functions involved in a single call (like beforeQuery and afterQuery callbacks or the like).

There's no one-better-way-fits-it-all.

deceze
+1  A: 

Between your solutions, first is better, but you have to change the names of the functions. 'get' should be used only if function returns something.

The reason it is better is that it doesn't use side effects, side effects always bad as they are invisible to user of the class but change class behavior. So you should try to minimize them or make them obvious as they are in the first case, when they not really 'side'.

But in this particular case, Get_Existing_User and Add_User should be static functions, that return new User object, it is sometimes called as static constructor. The reason why it is much better is that it makes it clear what that functions do, they get something as parameter (user_id of existing user or first_name, last_name and other attributes for a new user) and create an object that represents the user. All database manipulation will be hidden away. Object itself should have properties for name and other attributes and even Save() method to push the changes back. But main idea is that you always work with constructed object, object that already have context and linked to something in the real world (that is, user in the database), not an empty shell that will be filled in as you go.

vava
A: 

Some clarification on terminology first:

What you call class functions are more properly called methods. A method is a function on an object instance. Additionally, classes may have methods. These are called class methods or static methods. When you use the term class function, you are thus confusing the meaning.

That settled, there is no worse or better of the two approaches. You would use both, depending on the context. Parameters have a smaller scope, and thus cause less coupling. If everything else is the same, I would therefore say that parameters are preferable to setting an object property.

That said, there are usually other factors that can determine which to pick. You can think of an object as a scope for related variables. If a variable belongs to that scope, it would make sense to assign it to a property.

troelskn
A: 

It is important that you choose the method that best suits your situation. Ignoring that not-so-helpful suggestion I encourage you to take a good look at some important principles in Object Oriented Design

  1. Coupling
  2. Cohesion

A strong understanding of these topics will help you assess your situation and code to suit the goals of the project. As your project grows, you'll likely find that you'll want to use methods that have optional parameters to interact with your objects to achieve high cohesion and loose coupling. Then you'll use methods and parameters like an expert.

Good Time Tribe