Re: Attributes vs. Accessors
| From: | Christoph Boget | Date: | Mon, 25 Aug 2008 12:54:17 +0000 |
| Subject: | Re: Attributes vs. Accessors | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-278656@lists.php.net to get a copy of this message | ||
>> Curious. Which do you prefer and why?
>> For publicly-declared variables, do you access the attribute directly or
>> use an accessor?
> If it's a public member variable there is no need for plain accessor methods
> - they add no value. I feel the same about private variables with plain get
> and set accessors, there's just no point unless the accessors are doing more
> than setting and getting the internal variable.
Personally, I always use the accessor methods.
For the most part I agree with Stut -- they don't add much value.
Where it does add value, though, is in the eventuality you ever need
to change the attribute to a more restrictive visibility. If you ever
run across this need (and I have a couple of times), the only thing
affected is the class; you don't have to worry about changing all of
your code using that class. Conceptually, it is my preference to keep
a class as much of a black box as possible and accessor methods allow
for that.
thnx,
Christoph