I see (and write) a lot of code like this:
class MyClass
{
private $_myProperty;
public function setMyPropert($myProperty)
{
$this->_myProperty = $myProperty;
}
public function getMyProperty()
{
return $this->_myProperty;
}
}
Since we are taught that class properties should always be private.
However, i really just want to do this in the above scenario:
class MyClass
{
public $myProperty;
}
Thats much less code and easier to read. But other developers would look down on this code and most likely it would fail code reviews etc. Even if not, i would still never do this for fear of someone else seeing it and making judgement.
Why though? Is this something that is just ingrained in developers of oop code? Or is there another reason that I'm missing, perhaps related to testing, future maintenance or other non obvious technical reason. I am talking specifically in a scenario where the getter/setter does noting more that get/set.