Visibility
The decision whether to leave the public variable or not is the programmer according to the need. The fact that it is not used elsewhere does not mean that it cannot be used at some point. What many programmers don’t understand is that the class should be thought to be used in various situations. If you’re going to do something simple, which isn’t going to be used in many different ways and you won’t need complex maintenance, you don’t have to create a class.
The important thing is to understand why to do things. Handbooks serve to give a basis on the technique, but they do not teach people to think about problems and how to solve them.
The most common object orientation is to leave the variables private until you have a reason to make them public. But I think it is reasonable to understand that these data may be needed somewhere in isolation, especially if the class is just that, where it would be of little use. I would probably do it that way, if I were forced to create a class (this can be useful).
Doing OOP in PHP
Another important point is about methods to access class fields. Some say they should always be used. This makes some sense, but many people repeat this without knowing why it is necessary. Want to do so, ok, everyone can do as they please, but in PHP there is no need in most cases.
PHP is a language of script, the codes have Binding at runtime, so from a technical point of view it makes no difference to access the variable or a method, unless it is known that the method will need to be used in the future. Even in these cases I have my doubts if it is so useful for applications that PHP fits well (I know that many people make use of PHP where it does not fit, but there is another problem).
In most cases it is more interesting to initialize existing members with a constructor. Not always, but need to know when to use a constructor or not, can’t just follow a rule.
No longer declaring variables
If the question was about what the bfavaretto said in comment below (the AP showed that it is not), about the variable being created automatically without being declared, it is even possible, but it is not recommended, the reason to create a class is precisely to organize the code, declare what its function, its members, power is one thing, being certain is another. Some unpredictable things can happen.
In fact this is just one of the reasons I have objections to OOP in PHP, the language was not designed for this, it lets do a lot of wrong in the name of "simplicity", OOP does not go with this. In PHP an object is just a form of array associative that allows to insert and remove members at random.
I’m surprised at Unicamp teaching PHP, I hope it’s just some sort of slot machine course.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero