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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, May 23, 2011 at 6:29 AM, Simon Hilz <simon.hilz@xxxxxx> 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux