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?
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?"
– Costamilam
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.
– user126343
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
– Costamilam
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.
– user126343
"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/
– Costamilam
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?
– Woss
Thanks people.
– user126343