php - Getter & Setter also for in class usage? -


it bad way of programming, when access private/protected class members directly in class via getter/setter methods?

alternative #1

<?php  class {     private $myvariable;      public function getmyvariable() {         return $this->myvariable;     }      public function dosomething() {         $variable = $this->getmyvariable();     } }  ?> 

alternative #2

<?php  class {     private $myvariable;      public function dosomething() {         $variable = $this->myvariable;     } } ?> 

which way prefer? think first solution more readable in constrast second one. please let me hear opinions.

thanks in advance.

since determined not duplicate, copy points this response relevant case:

  • encapsulation of behavior associated getting or setting property - allows additional functionality (like validation) added more later.
  • controlling lifetime , memory management (disposal) semantics of property - particularly important in non-managed memory environments (like c++ or objective-c).
  • providing debugging interception point when property changes @ runtime - debugging when , property changed particular value can quite difficult without in languages.
  • allowing inheritors change semantics of how property behaves , exposed overriding getter/setter methods.

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -