Re: Attributes vs. Accessors
| From: | Stut | Date: | Mon, 25 Aug 2008 12:58:35 +0000 |
| Subject: | Re: Attributes vs. Accessors | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-278658@lists.php.net to get a copy of this message | ||
On 25 Aug 2008, at 13:54, Christoph Boget wrote:
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/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.]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.