In Object Orientation getters and setters participate in the Principle of Withholding Information, which ensures that a property is available for reading or writing only in certain circumstances:
- Private: Only who defined
- Protected: Who defined and who to extend
- Public: Everyone
However, both getters how much setters are optional. And this allows you to simulate read-only and even write-only properties (if there is any practical application for this) which, until now, cannot be done automatically at the level of interpretation:
In your example, set visibility of the property Url::$url as private ensures that no one can change its value directly.
If she had visibility public, this could occur:
$url = new Url;
$url -> url = 'Oi, eu sou Goku!';
And anything that relies on a valid URL would fail.
Therefore, you have to ensure that only Urls are informed as a value for this argument because through a Setter, you can validate.
If this value needs to be read in the context of the instance, you should add a getter. But if that argument can’t be altered in the same context, you don’t need and shouldn’t even have a Setter because once defined the URL it must be kept the same until the object is destroyed manually or at the end of the Request.
However, although the constructor of an object serves to build the object (Doh) he must not do everything alone.
The ideal is to delegate the task of validating and assigning value to the property through a Setter.
But you’re contradicting yourself!
Yes, it may look like this, but just as properties have configurable visibility, so are they for methods, so you can have a method specifically created to validate and set a value to that property, but not available externally:
class Url {
private $url;
public function __construct( $url ) {
$this -> setUrl( $url );
}
private function setUrl( $url ) {
if( filter_var( $url, FILTER_VALIDATE_URL ) === FALSE ) {
throw new InvalidArgumentException(
sprintf( '%s is not a valid URL', $url )
);
}
$this -> url= $url;
}
public function getUrl(){
return $this -> url;
}
}
Notice, too, the difference in this code compared to yours. You accepted an invalid URL which forces you to manually check that this value is a valid URL when your class needs to use the value of that property again and again and again.
And that defeats the purpose of a Setter which is precisely to ensure that the past information is reliable.
In this example, if the URL does not pass the validation I trigger a Invalidargumentexception, a specific natively available exception to be used when a given argument is invalid for the purpose of who defined it.
Good studies :)
I improved the wording of the question but it is worth it to you to review it because in one part you say you will use the value outside the class and in another moment you say you will not need the getter because the value will only be used within the class. I have not touched this part (to add a nay to the first statement), not to radicalize its intention.
– Bruno Augusto
I meant that I will use Setter outside the class, but I could directly use the $url variable, without the getter, got it?
– Thiago