On 25 Aug 2008, at 13:54, Christoph Boget wrote:
]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.
In my experience that's pretty rare, but your mileage may vary. If it
does happen you can simply implement __get and __set magic methods,
which is far better from a code maintainability point of view than
littering the class with pointless accessor methods just in case (IMHO).
-Stut
--
http://stut.net/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php