0

Possible Duplicate:
Why use getters and setters?

Which method is better practice?

$user = new User();
$user->username = "bob";
$user->password = "password";
$user->age = 42;

echo $user->username;

vs.

$user = new User();
$user->setUsername("bob")->setPassword("password")->setAge(42);

echo $user->getUsername();

Is it better practice to access and set object variables through methods or directly? User is an object model for users. Thanks in advance.

Community
  • 1
  • 1
axsuul
  • 7,370
  • 9
  • 54
  • 71

3 Answers3

2

It is better to use encapsulation, which is one of the biggest advantages of OOP. Make your attributes private and use setters and getters to access your attributes.

If you use setters you can do checks before setting a value to an attribute. For example: If you want to make sure the $id attribute is an integer you can check it in your setter. If you use direct access to your attribute you can't. If you want to change a check for an attribute, you only have to change it at one location.

Because using setters is the way to go, you should also use getters, otherwise there is still a possibility to set your attributes without using the setter.

Please have a look at the magical __set() and __get() methods to make your life easier when it comes to setters and getters.

More information about __set and __get (and other magic methods): http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

Edit

I do agree with Gordon. But still it's worth noting the possibility of using magic methods.

@Axsuul: When you define a property use the private, public or protected keyword. For example:

protected $id;

or

private $id;

From

http://php.net/manual/en/language.oop5.visibility.php

"Class properties must be defined as public, private, or protected. If declared using var without an explicit visibility keyword, the property will be defined as public."

There is no option to change the default visibility.

Rene Terstegen
  • 7,911
  • 18
  • 52
  • 74
  • The magic methods are not a substitute for proper getters and setters and using them for that purpose is significantly slower than using regular functions. There is also a lot of us who believe in using protected instead of private as the default visibility. – Gordon Nov 03 '10 at 09:17
  • How do you set the default visibility? – axsuul Nov 03 '10 at 09:38
  • @Axsuul Sorry, if this was unlear: what I mean with *default visibility* is which keyword to use when writing your class. Like Stegemann correctly points out, the internal default visibility cannot be changed. It will be public unless you declare different. – Gordon Nov 03 '10 at 10:12
2

Using private attributes with setters gives you the opportunity to implement some basic validations against the properties, so if (for example) name and password are mandatory, you can check whether they are called with a valid value and throw an exception if not. Otherwise, you have to do this elsewhere in your code.

Forcing the use of getters also allows you to perform other tasks (perhaps formatting phone numbers, or ensuring that names are displayed with an upper-case first character, for example), before returning the value.

There are real benefits to this, because if you choose to change your password rules, it's obvious where to do it... and if password setting is called from multiple locations within the rest of your code, then it's a single point of validation logic.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

using methods (getters and setters, making the atributes private) is the 'correct way' called encapsulation.

oezi
  • 51,017
  • 10
  • 98
  • 115