开发者

Is it better practice to access an object's variable directly or through a function? [duplicate]

This question already has answers here: Closed 12 years ago.

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 开发者_如何学Cor directly? User is an object model for users. Thanks in advance.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜