Methods with several PHP parameters

1

I have a form where I will store the data of the employee's medical record in a database. The problem is that this form has more than 30 fields. I'm using OOP to register this way:

$metodos->cadastrarFichaMedica($idFuncionario,$tipoSanguineo,$planoSaude,$calendarioVacinal,$nomeContatoI,$telefoneFixoI,$nomeContatoII,$telefoneFixoII,$nomeContatoIII,$telefoneFixoIII,$viveCom,$doencasTeve,$alergias,$qualOutrosAlergia,$atrasoDesenvolvimento,$qualOutrosAtraso,$pcd,$problemasCoracao,$qualProblemaCoracao,$acompanhamentoProblemasCoracao,$qualAcompanhamentoProblemaCoracao,$alergiaMedicamento,$qualAlergiaMedicamento,$intoleranciaGluten,$qualIntoleranciaGluten,$tomaMedicamento,$cirurgia,$ficouInternado,$problemasPeso,$qualTratamentoEspecializado,$qualTratamentoEspecializado,$observacoes);

Class page:

public function cadastrarFichaMedica($idFuncionario,$tipoSanguineo,$planoSaude,$calendarioVacinal,$nomeContatoI,$telefoneFixoI,$nomeContatoII,$telefoneFixoII,$nomeContatoIII,$telefoneFixoIII,$viveCom,$doencasTeve,$alergias,$qualOutrosAlergia,$atrasoDesenvolvimento,$qualOutrosAtraso,$pcd,$problemasCoracao,$qualProblemaCoracao,$acompanhamentoProblemasCoracao,$qualAcompanhamentoProblemaCoracao,$alergiaMedicamento,$qualAlergiaMedicamento,$intoleranciaGluten,$qualIntoleranciaGluten,$tomaMedicamento,$cirurgia,$ficouInternado,$problemasPeso,$qualTratamentoEspecializado,$qualTratamentoEspecializado,$observacoes){
// Faço o cadastro
}

The problem is that it got too big and I wonder how I can fix it? My idea was to split into two or more parts, but how could I do it within the good programming habits?

    
asked by anonymous 15.09.2018 / 23:50

2 answers

1

Reducing the number of parameters

You can create an object to group all of this data or use an array to do so. Each of these approaches has its pros and cons, but both serve as a better alternative than a huge amount of parameters.

Signature of the method using an array:

public function cadastrarFichaMedica(array $dadosCadastro){
 // Implementaçao para fazer o cadastro
}

Signature of the method with an object that groups the parameters:

public function cadastrarFichaMedica(Ficha $ficha){
 // Implementação para fazer o cadastro
}

In the case of the second example, the object would be an entity used to gather all the data and give a more readable meaning to them. This is beneficial so you do not have to change the method signature if a new value comes up.

    
16.09.2018 / 00:58
2

Create a class with all properties

class FichaMedica {

public $idFuncionario;
...

I would also recommend using the properties by $classe->propriedade instead of creating a setPropriedade function or using magic methods to get by performance question .

Then change the method to receive only one parameter

public function cadastrarFichaMedica($fichaMedica){}

Depending on the php version, you can still type the function like this:

public function cadastrarFichaMedica(FichaMedica $fichaMedica){}

In general, if I see that a function has more than 4 parameters, I analyze and try to break it into more functions or refactor the function logic to accept for example a class that will hold the values.     

16.09.2018 / 00:05