How to split the addressing of a class into PHP?

Asked

Viewed 172 times

0

What is the best way to divide this Aluno?

<?php
    class Aluno{
        private $nome;
        private $sobrenome;
        private $email;
        private $telefone;
        private $cep;
        private $rua;
        private $endereco;
        private $numero;
        private $logradouro;
        private $cidade;
        private $estado;
        private $usuario;
        private $senha;

        public function getNome(){
            return $this->nome;
        }
        public function setNome($nome){
            $this->nome = $nome;
        }

        public function getSobrenome(){
            return $this->sobrenome;
        }
        public function setSobrenome($sobrenome){
            $this->sobrenome = $sobrenome;
        }

        public function getEmail(){
            return $this->email;
        }
        public function setEmail($email){
            $this->email = $email;
        }


        public function getTelefone(){
            return $this->telefone;
        }
        public function setTelefone($telefone){
            $this->telefone = $telefone;
        }

        public function getCep(){
            return $this->cep;
        }
        public function setCep($cep){
            $this->cep = $cep;
        }

        public function getRua(){
            return $this->rua;
        }
        public function setRua($rua){
            $this->rua = $rua;
        }

        public function getEndereco(){
            return $this->endereco;
        }

        public function setEndereco($endereco){
            $this->endereco = $endereco;
        }

        public function getNumero(){
            return $this->numero;
        }
        public function setNumero($numero){
            $this->numero = $numero;
        }

        public function getLogradouro(){
            return $this->logradouro;
        }
        public function setLogradouro($lougradouro){
            $this->lougradouro = $logradouro;
        }


        public function getCidade(){
            return $this->cidade;
        }
        public function setCidade($cidade){
            $this->cidade = $cidade;
        }


        public function getEstado(){
            return $this->estado;
        }
        public function setEstado($estado){
            $this->estado = $estado;
        }

        public function getUsuario(){
            return $this->usuario;
        }

        public function setUsuario($usuario){
            $this->usuario = $usuario;
        }

        public function getSenha(){
            return $this->senha;
        }

        public function setSenha($senha){
            $this->senha = $senha;
        }



    }
?>

Would it be relevant to separate some information in another class as a database table? Type:

Class Telefone{
       private $ddd;
       private $numero;

      public function getDDD(){
        return $this->ddd;
      }
      public function setDDD($ddd){
        $this->ddd = $ddd;
      }
      public function getTelefone(){
        return $this->telefone;
      }
      public function setTelefone($telefone){
        $this->telefone = $telefone;
      }

I must apply these classes and methods in the state (Class Estado get and set state), city (Class Cidade get and set city), address (Class Endereço containing number, zip code, complement, lougradouro getters and setters etc.).

That would also apply with Usuário and password create a class only for both?

Example:

Class Estado{
       private $estado;

          public function getEstado(){
            return $this->estado;
          }
          public function setEstado($estado){
            $this->estado = $estado;
          }
}

Class Cidade{
       private $cidade

       public function getCidade(){
         return $this->cidade;
       }
       public function setCidade($cidade){
         $this->cidade = $cidade;
} 

Etc..

NOTE: When I model something of this type, have some specific technique to separate correctly or will depend on the programmer?

  • 2

    First, ask yourself, "Why am I using classes?" then, "Why am I leaving all the attributes private and making 1 getter and 1 Setter for each one?" and then "what are the advantages and disadvantages of being in the same class, and of being divided?"

  • I asked myself, and the question arose... Does a class really contain all these attributes and methods? An example, if I put everything in Pupil would be huge, wouldn’t it? Private attributes for "security" reasons, however simple... My advantages and disadvantages would be the one of my question regarding you rs.

  • 1

    Why is it safer to leave private if you have two methods that give full access to this attribute? In your example of a Phone class, it would be useful if the Student can have more than one phone, so the class would have an array of Phone objects, but why create an object that will only have two attributes? A simple string array would not suffice? One tip is, try to do everything as simply as possible, only add complexity when this complexity actually brings some relevant benefit

  • I understood, but if I leave the private methods will not be able to communicate with another Class. On the complexity, would try to make it more difficult to build and facilitate when executing... The last question, do OO classes work like a database table right? That’s why this question arose... If by chance I had done tables in a bank it would be this to maintain the integrity of the data. But I do not understand to the bottom where goes the limit that I should apply in a class... Anyway, thank you for the answer William.

  • 2

    "classes with OO function as a certain database table?" No, tables cannot implement or extend other tables, but you can create a class that represents the structure of a table, important milling that OO goes far beyond using classes, in these classes I do not see OO. I recommend reading: https://answall.com/questions/151323/, https://answall.com/questions/104340/ and https://answall.com/questions/25995/

  • 2

    It depends on the requirements of your project. Can a phone exist without a user? Can a user own more than one phone? Can more than one user own the same phone? The same for the address. Have you raised all the requirements that your code needs to meet?

  • Thanks people.

Show 2 more comments

1 answer

2


Already commented but I want to reinforce. You are programming as if you were in Java. In PHP you usually do not need any of this. PHP is a language of script, It was created to make simple code and people started creating complex code, and by the way, unnecessary. In general they don’t know why they’re doing it, only because they saw someone doing it somewhere, without understanding the reality and context of who did it. Probably this person did it for the same reason you did, so only because you saw someone else doing it. It’s a Simão Monkey joke that has nothing to do with engineering, which is our professional area.

Another problem with the question is to want to know the best way of random people on the internet who do not know their problem. Or even worse, your problem could be artificial. People think that "solving" artificial problems teach developing software, but it doesn’t teach, at most it teaches using a language mechanism, which is not the case with the question, it wants to know about data structure organization, which is only useful when you know 100% of the requirements and correctly. We don’t know.

Also, spend how each is accustomed to do, I would erase everything and start in a totally different way, and simple.

Only you know if it would be relevant to separate into parts. Can you justify this? Will there be gain? Does it serve any purpose? Don’t you have a better way to do that? For what you have shown you do not need, but it may be only that you have shown wrong to us, if you have done so any attempt at help will be wrong.

Modeling a data structure depends essentially on current requirements and the possibility of future requirements. Modeling is experience. It’s taking real, detailed cases and seeing if it works or not over time and learning from mistakes, or even using other people’s experience to show where mistakes are, as long as you have all the necessary information. Modeling has to do with fitting logical thinking. It’s understanding things that aren’t even computing.

The responses to the comments show that you’re stuck with a vision of doing things. That’s why I hate the way people put things on the Internet, they ruin people’s minds forever. The person can no longer see another way to do it, everything becomes a meaningless cake recipe.

There are controversies about what classes are, but in general they are the opposite of database tables, even though they look the same. I kind of wanted them to be used in a similar way, but that’s not how people use it, but the subject is too broad to discuss here. Anyway this case seems to be separating the individual data into separate tables, this seems very wrong, but it may just be because there are no clear requirements in the question.

  • Jeez. Thanks for the full reply Maniero... So that no more doubts occur as this could you recommend me something to study? I am a student of technical education and have not learned much in depth about all this, and I would like to know more and expand my knowledge, if you have some books that can help me in my journey thank you. Sorry for the inconvenience!

  • Worse than not, these things are not so organized. Here are many answers that talk about these things.

  • I get it, thanks. I’m going to look around.

Browser other questions tagged

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