Getters and Setters are not limited to provide access to the class’s attributes(variables) rather it is one way of encapsulating the data. Getters and Setters are not the PHP thing which will be dead rather it is an approach to encapsulate data.
Advantage Of Encapsulating Data
Encapsulating data provides a clear point to monitor changes and use of the data; I can easily add validation or consequential logic on the updates. It is my habit to make all mutable data encapsulated like this and only accessed through functions if its scope is greater than a single function. The greater the scope of the data, the more important it is to encapsulate. My approach with legacy code is that whenever I need to change or add a new reference to such a variable, I should take the opportunity to encapsulate it. That way I prevent the increase of coupling to commonly used data.
This principle is why the object-oriented approach puts so much emphasis on keeping an object’s data private. Whenever I see a public field, I consider using Encapsulate Variable (in that case often called Encapsulate Field) to reduce its visibility.
“Encapsulate Variable” Refactoring By Martin Fowler
Getter Setter Example
<?php
class Person
{
private $name;
private $personalSiteURL;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getPersonalSiteURL()
{
return $this->personalSiteURL;
}
public function setPersonalSiteURL($URL)
{
$this->personalSiteURL = $URL;
}
}
$aPerson = new Person();
$aPerson->setName('Tanmaya Biswal');
print $aPerson->getName() . "\n";
$aPerson->setPersonalSiteURL('https://sakeoflearning.com');
print $aPerson->getPersonalSiteURL() . "\n";
Setter is more than storing and validating types
Setters are not just for setting values. As mentioned earlier, setter can be used to perform extra business logic based on the value that was passed to the setter. We can properly validate the passed value and throw the appropriate exception that could be handled by others. Let’s say that I was expecting a URL, but not just any URL. The passed URL must be starting with https and be using one of the pre-authorized hostnames.
<?php
public function setPersonalSiteURL(string $URL)
{
if (filter_var($URL, FILTER_VALIDATE_URL) === false) {
die('Not a valid URL');
}
$this->personalSiteURL = $URL;
}
Conclusion
As long as Encapsulation is there, Getter and Setter will serve the developers!
One of the prime features of objects is encapsulation—hiding internal details from the rest of the world. Encapsulation often comes with delegation. You ask a director whether she is free for a meeting; she delegates the message to her diary and gives you an answer. All well and good. There is no need to know whether the director uses a diary, an electronic gizmo, or a secretary to keep track of her appointments.
From Martin Fowler
2 Comments
Arun · August 11, 2022 at 5:17 am
Thanks for sharing.
Tanmaya Biswal · August 14, 2022 at 6:20 am
Sure! Thanks 🙂
Comments are closed.