What you usually read are called good practice. The person says what you should do without saying why, often because the person doesn’t even know why. It is common even to be just repeating what she "learned" without questioning why.
We should always use the attributes of a class as private?
I don’t think so. It depends on a number of circumstances and the language you’re using, not to mention the philosophy you’ve adopted.
What private variables help prevent?
Prevents other classes from accessing that variable directly, as you already know.
How do you decide whether a particular property should be private or not? CASE, by default, each private FOR field, so why there are public data members in a class?
Each has a criterion. Some people decide that all will be private Some decide to make public only those who need maximum performance. Others choose to make public all variables that only access their value in a simple way without processing. Others analyze the domain as a whole to make a decision.
Again all of this depends on the language you are using, on the ancillary technology that may require a certain form, on the philosophy adopted, on the maintenance requirements, on how the class will be consumed, just to name the main ones.
Each one has maintenance implications if one day you need to change. Everything public is part of the API, changing that can be traumatic. But it depends. If you have full control over the application this is not so difficult so if you have the right tools.
There are cases where you want the variable really to be just an implementation detail, then it has to be private, everything that is public is part of the contract that you need to look at most of the situations, but not all as some people think. Needs to be pragmatic.
Under what circumstances a variable shall be made public?
Whenever you think the toilet is not suitable, within the parameters already passed above.
Has a question with more information and links important to deepen on the subject.
Complement after editing the question
Now that the language-independent and placed the tag of php I would say there’s no need to use private variables, at least if you have a getter and Setter that do nothing but return the value or assign the value passed. Remember that PHP is a language of scripts.
Some people think that, even in the language of script techniques used in languages Nterprise.
On the other hand the gain of using the public variable is quite small in PHP. The class variables in PHP are the same thing, there are no optimizations. Still I prefer not to put lines of code I don’t even know if they will be useful.
Note that using magical methods is different from using methods getXXX()
and setXXX()
since then you will have to change the consumer code if you need to include some processing in the access to these variables. But reinforcement that one is making one script, this is quiet. You are using PHP to do scripts, right?
Of course, if the variable is genuinely only to be accessed by the class, if it is not creating an artificial encapsulation, then the use of the private variable is interesting.
Private variables help achieve cohesion and avoid coupling unintentional.
I did an answer about encapsulation addressing more the application side of business rules to attributes, maybe it is useful to you at something.
– Woss