NYCPHP Meetup

NYPHP.org

[nycphp-talk] Getter/setter best practices

Rob Marscher rmarscher at beaffinitive.com
Tue Jan 12 12:12:39 EST 2010


On Jan 12, 2010, at 11:58 AM, Yitzchak Schaffer wrote:
> What's BP for changing the values of protected properties within a class that also has public getter/setters?  e.g. in Foo::doSomething() below:
...
>  protected function doSomething()
>  {
>    $new_value = $this->getNewValue();
> 
>    $this->bar = $new_value;
> 
>    // OR
> 
>    $this->setBar( $new_value );
>  }
> }

If you go through the effort to create getter/setter methods, you should always use them.  Otherwise, you lose the benefit of having them - which is that more logic can easily be placed in the getter and setter methods to do something more than simply assign/retrieve the variable.

-Rob




More information about the talk mailing list