Access modifiers in PHP

Asked

Viewed 182 times

3

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?

Example:

    class Pessoa {

      private $nome;
      private $idade;
      private $sexo;

      private function falar() {

       echo("Falando...");

    } 


      public function correr() {

       echo ("Correndo...");

    }

      public function mostrarMétodo() {

      $this->falar();

    }
}
  • What is the advantage/disadvantage of this? (If possible, real example)
  • Another question, why leave the attribute private (private $nome), and access through the method?
  • 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.

3 answers

5

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.

Monte de peças lego soltas

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.

  • Thank you for the answer, I am learning now OPP and I have not had many orientations, LP learned basic commands (if, Else, Else if, switch, array, etc.) and not advanced data structures so I think with the mistakes we have learned... Of course it is my opinion, but I consider your reply to be valid. In fact, I’m following video lessons on several channels, so I don’t know if it’s right or not, by the way, this is relative...

  • As you have experience in the area (besides the MVC awards), could you help me with books, videos, channels etc? This already helps enough new developers who are entering the market or study zones...

  • I don’t really like to indicate these things in general, because not only do I indicate, I need to do a full review, it shows that there are negative points, because if I sign under something that has problems I am helping the person to have problems. I will do it yes, this is in my Todo list, but when I will do I do not know, lack time, follow me in the nets that one hour I put some.

3

It makes sense, when designing a class the ideal is to define methods (functions) as being behaviors of a given entity and not simply multiple getters and setters to modify attributes. A method can encapsulate a set of rules and validations so that it consistently defines class attributes, whereas a public method can execute several other private internal methods with the intention of isolating their implementations and ensuring that these internal methods are not called outside the class.

-3

Good afternoon,

What is the advantage/disadvantage of this? (If possible, real example)

This allows you to keep your system’s critical operations secure, such as viewing a password, entering balance.

why leave the attribute private (private $name), and access through of the method?

This prevents the name attribute from being accessed and/or assigned directly without any validation. Imagine that you have a $balance attribute This attribute cannot be simply used:

Pessoa->$saldo = 1.000.000

by accessing through a method you ensure that all necessary rules and validations have been made

  • 1

    How would something private prevent you from viewing a password? If the attacker could perform something like echo $obj->senha it could very well access the source file and look at the password.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.