I'm new to PHP, and whenever I'm reading up on good practices, especially relating to OOP, I often see statements such as "don't expose local variables to the global space, use a getter method or retrieval method instead".
I understand why we don't want to pollute the global namespace, but at what point does it become simply ridiculous to call a getter method just to access a simple property? I think this shows massive hypocrisy. We're willing to expose and call methods outside of the class definition, but not simple variables? Why is this? Isn't a method much more complex?
Forgive me if my confusion is missplaced. I'm genuinely interested in understanding OOP best practices.
It's always a best practice to use getters and setters to access object properties because they hide information about how you store and get a particular value.
Basically you provide an interface (that will not change) to access information via an hidden logic (that could change).
A silly example could be a getTotal
method that return a calculated value. You could have a simple getter like this, that simply returns the previously calculated total:
function getTotal()
{
return $privateTotal;
}
Or you can calculate the total inside your getter:
function getTotal()
{
return $sub1 + $sub2 + $sub3;
}
You can change implementation at any time. The client will not be aware of the changes in your logic.
This is referred to as encapsulation. Local variables are, generally, an implementation detail. This shouldn't be exposed because calling code (if it is correctly decoupled) only cares about what you do not how you do it.
If your implementation changes variables may be removed or changed meaning calling code would need changed. Methods abstract this and allow the internals to change without affecting the exposed interface.
Another reason is that direct access to variables allows calling code to alter variables that the class relies on. Methods allow creating a copy of, say, an array so that code which changes the array doesn't affect the logic of the class.
When you use a method, this allows classes to override the method. The property might be implemented as a private variable in some classes, but as a more complex computation in others. If you expose the property directly, you're stuck with it in all classes that implement the same interface (well, not really, since you can fake it with magic methods, but this should usually only be used as a last resort).
Forget global namespaces for a second and have a look at the underlying concepts of encapsulation and information hiding. In OOP you construct your software out of smaller components (e.g. classes). You can not ensure that a class is working properly when you expose its (fragile) internals to the outside. Getters and setters allow you to control the access to variables you want to expose to the outside of the class.
The question is not Why should you write useless getter / setter methods but Why should you expose variables to confuse about the main purpose of the class and its methods?
Consider that variables can have only getters, only setters or neither of them.