Question

I am making a new class in PHP. I don't anticipate this class ever being extended. Should I bother with making class members private and implementing getter and setter functions?

Part of me thinks that this is just a big waste of time and only serves to bulk up my code.

The class is for a resume. I am writing it in code to demonstrate my coding style. The question is will an employer want to see getters and setters, or will that just clutter things up?

Was it helpful?

Solution

I wouldn't bother if I were you. You're right, that decoration would just clutter up your class.

OTHER TIPS

I don't want once again to repeat the argument that even that you don't expect inheritance now, you may still need it in the future, but they are still more reasons to use getters and setters:

  • Getters and setters will let you easily implement validation of your properties
  • Your code will follow good coding guidelines and as such it will be easier to be read by others. If I ran into a code file that didn't use getters and setters, I would be very skeptical about it and would have to read it carefully many times.
  • Also this is not only a per class decision, but a decision for your whole project. Do you want to have getters and setters for some classes you need them and not for some others? I believe that consistency is important.
  • There are tools that can autogenerate writing the getters and setters for you. Remember that you write the code only once, but you read it many times,

Check out the __get and __set magic methods, it'll keep your code clean and you won't have to feel lazy for not setting variable scopel.

public function __get($name)
{
  return $this->$name;
}

public function __set($name, $value)
{
  $this->$name = $value;
}

Obviously you'd have more detail, but that will handle your accessors.

You may never know that you will not need to extend the class in the future. What will the class be doing? How complex it is?

Encapsulation is part of good OO design. But good OO design is not the goal of programming. The goal is to have things working and be able to modify them easily when needed.

You can just override __get & __set as far as I understand. http://php.net/manual/en/language.oop5.overloading.php

I like to make the properties in my class protected or private and then use getters and setters. I then use code in the setters to make sure that the property values are within accepted ranges. I like this approach as I find it makes debugging easier as I can always be sure that an accepted value is being set, or an exception is being thrown. It also gives me more confidence that when I pass objects around the values held in the properties are not going to cause anything else to blow up.

If it's for a resume, it's likely that they care just as much (or more) about your logic as they do the little details like getters/setters.

If it was me, I'd make them public without getters/setters, but include a comment above them, saying something along the lines of "consider making these private and using getters/setters depending on the scope of the application and preferred code style of the team"

That way:

  1. Your resume code isn't cluttered

  2. They understand that your not just lazy and making everything public for no reason

  3. It shows that instead of blindly following a particular style, you'd consider the particular application in making a decision.

Do you want to do someting special on set or get (or just one of them private)?

If not, then don't waste your time on these.

You don't gain anything on doing this if you don't use them in a "special way" and maybe it's even slowing everything down

If you don't plan to have specific behavior when setting or accesssing those property, it doesn't mater much.

If you are required to have specific behavior when accessing or modifying those property later you can always overload the __set and __get method.

See : http://php.net/manual/en/language.oop5.overloading.php

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