Does it make sense that I create a class, define three functions, two public and one private, and make the private function be accessed through the public method?
In the general makes a lot of sense, public methods are part of a contract, the API, is what you let others access in your object, already private methods are implementation details, are parts you do not want to be known and accessible outside the object, is something you can change whenever you want and have no commitment to always keep so, is not part of the object contract, is only to facilitate the organization of your internal code.
Specifically in the example does not make sense, at least I think, I can only affirm seeing the requirements. Intuitively I think that the fala
should be public, and mostrarMétodo()
looks like just misguided example, so it seems to make more sense:
public function falar() {
if (!$mudo) articulaFala();
}
private function articulaFala() {
echo("Falando...");
}
I put in the Github for future reference.
So does it make sense? Depending on the requirement, it may be that this private auxiliary method is not necessary, just the specific case to know. So I always say that programming is not following rules, it’s not putting a lot of lego together, it’s understanding what you’re doing.
What is the advantage/disadvantage of this? (If possible, real example)
When I see this kind of question I already know that the person started learning OOP without having learned basic programming, ie, is creating finishing in the house that has no foundation. I realize that most people create functions all the time and they don’t know why they’re doing it, she saw a cake recipe that does so and does the same. Only by understanding the basics of programming can you program correctly. You need to understand why break the code into parts, and what those parts would be, what’s little, what’s much, what’s in it for you. We’re talking about DRY, of abstraction.
Then you can answer because it must be public or private. Being public is easy, it’s because you need it to be accessible by external objects, only these methods should be public, if you don’t have to, don’t do, everything public becomes contract, you have to fulfill forever, you can’t change anything that breaks other codes. You have to avoid it to have good cohesion and low coupling. And the private must hide everything that doesn’t need to be part of the contract.
I’m reciting the basic rule. Who really knows how to program knows the time to use one or the other and depends on a lot of study, a lot of correct practice, a lot of ability to evolve, to pay attention to the details and want to do right, something that is very rare nowadays. Luckily today when we have questions we can ask random people on the Internet, and in some cases we find people who can evaluate correctly and can teach us if we did it right or not, since we were able to ask the question in the right way by providing every possible detail. The problem is that as we do not know well on the subject we have difficulty in assessing whether it is right or not, and we always live within a Dunning-Kruger. Sure, some people don’t even want to really learn, just know the cake recipes.
There’s only a downside if you do it wrong, if you don’t answer what you need or it’s called overengineering.
Half or abstract examples do not help much here and as I have not programmed in PHP, mainly not use OOP in a language script (In this kind of language does not make sense, I do not know why people do this, except for being trendy), nor do I know good ready examples that demonstrate this (there is, only it is not in my day to day).
Another question, why leave the attribute private(private $nome
, and access through the method?
This has been widely answered. But in short don’t do anything you can’t justify why you’re doing.
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