Question

I am creating a User class and it is supposed to be an Entity class. In DataBase i created User table and it has 20 fields.

My question is : Is it good to create a "__construct()" with 20 parameters or i should user setter/getter?

I recently read some article about dependency injection and how it makes code more maintainable but i am confused in my own code. Here it is my __construct function:

class User{ 
    private     $userName;
    private     $userLastName;
    ...//Other users fields

    public function __construct($name,$lastname,$phone,...){
           $this->userName = $name;
           $this->userLastName = $lastname;
           ...
    }

}

I am looking for best practice to write some readable,clean and maintainable code.

Regards

Was it helpful?

Solution

This has nothing to do with dependency injection. Here you wouldn't be injecting dependencies, but just values.

So the real question is: should you get those values in a constructor, or through getters/setters?

And I'd say using your constructor is a good thing, because it can prevent your model to exist in an invalid state. For example, if you user need to have an email, then by putting it in your constructor your are guaranteed that all users will have emails.

So I suggest that you put in your constructor all the properties that needs to be defined (not null), and use setters for all others (that will help avoid having a 20 parameters constructor).

OTHER TIPS

By having 20 parameters there is a big chance that your class in question violates the Single Responsibility Principle and forms some sort of monolithic class without properly delegating, composing functionality.

Ensure that your class has a low coupling and a high cohesion.

See http://500internalservererror.wordpress.com/2009/02/23/what-do-low-coupling-and-high-cohesion-mean-what-does-the-principle-of-encapsulation-mean/

As we are not doing full code review here (go to https://codereview.stackexchange.com/ instead)

I suspect you are passing things like country, zip, phone number etc all as parameters. Ask yourself what data is absolutely required to construct a valid object.

If you have a class Person which requires a property called Name then having a setter for Name but not a ctor argument allows you to create incomplete classes. This shifts the burden of maintaining consistency to every method as you cannot be sure that Name has been set. Such code is bad and typically results in carrying some "initialized" flag or a function and clutter your code with checks.

Your ctor should contain the minimal set of parameters. Everything else can be done via properties or being further composed, e.g. an Address class removing the complexity of passing zip, state, street, etc all in the parent ctor. You may create a "Contact" class storing email and phone numbers, etc.

It's better to use getter and setter. A __construct method with 20 parameters is to huge. And without getter how you want to access to your private User attributes ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top