Member-only story
Implementing Getters and Setters is contrary to the Single Responsibility Principle.
![](https://miro.medium.com/v2/resize:fit:700/0*TSKi0bEr8lyG5gwF.jpg)
Not a Medium Member? Read this article by clicking here.
Software Engineers need to consider the purpose of certain pieces of code. We all should aim to deliver maintainable code while it’s secure and does not perform side operations (we can do that with automated testing).
And, be honest, getters and setters are some of the most useless practices someone decides to implement (only because other people do it).
Please keep reading before yelling at me in the comments.
Let’s consider a sample code as follows:
class MyAwesomeClass
{
private string $property;
public function setProperty(string $value): void
{
$this->property = $value;
}
public function getProperty(): string
{
return $this->property;
}
}
$myAwesomeClass = new MyAwesomeClass();
$myAwesomeClass->setProperty($value);
var_dump($myAwesomeClass->getProperty());
The purpose of a setter is to “set a value to a property,” and the getter aims to “get a value from a property.” As simple as that. That’s why they are called getters and setters.
This means we can refactor all that logic with a much simpler one:
class MyAwesomeClass…