[PHP] best practise accessing object's attributes from objects itself

2011-05-23 Thread Simon Hilz

hi,

i was wondering if there is any best practise known how one should 
access the attributes of an object from the object itself.
i mean, it is a good practise to write getters and setters for the 
attributes of an object to its interface. but is it common to modify the 
attributes from the object itself directly or also through the interface 
methods? i use the interface methods in my own classes at most times but 
recently i dived into zend framework and there it seems not to be usual. 
as zend framework is more or less a showpiece-software in php 
programming i'm not sure if my practises are good. are there any 
discussions by really focused php programmers about that?



Simon

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] best practise accessing object's attributes from objects itself

2011-05-23 Thread David Harkness
On Mon, May 23, 2011 at 6:29 AM, Simon Hilz simon.h...@gmx.de wrote:

 i was wondering if there is any best practise known how one should access
 the attributes of an object from the object itself.


For most properties I use $this-property within the object because nine
times out of ten no work ever needs to be done with them. Usually they are
set by the constructor and optionally changed externally using a setter. As
long as only the class and subclasses access properties in this manner, it's
easy to change them to use an accessor later if necessary. I never access
the properties directly from unrelated classes.

When a property needs to have logic applied--either during get or set--I'll
use accessors even inside the class to ensure that work is done consistently
and avoid repetition. In some rare cases (testing framework) I use __get()
and __set() so subclasses can use $this-property but get redirected through
the accessor. This isn't possible inside the class, however, because the
magic functions are only invoked when the caller cannot directly access the
property.

David