Methods with multiple PHP parameters

Asked

Viewed 74 times

1

I have a form that will store the employee’s medical records in a database. The problem is that this form has more than 30 fields. I am using POO 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 it’s gotten too big and I’d like to know how I can fix it? My idea was to divide into two or more parts, but as I could do within the good programming habits?

  • 2

    Break in lines. PHP doesn’t have to have everything in one line. It’s a good habit to know how to program only. The best habit is not to use POO in PHP, because it is a waste of resources - think to me, the objects serve to keep state, and PHP does not. And if it is to use POO, it is useless to mix everything in the class. This is not POO, it is a disguised function (Phpeiro does it a lot). Make everything cute with functions, it gets better. When you have a domain name, you can use POO (but when you have a real domain name, you’ll see that it’s best not to use it in PHP. You can see that only crap comes out in POO with PHP practically)

2 answers

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 function setPropriedade or use magical methods to get by performance issue.

And 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 function logic to accept for example a class that will hold the values.

  • Thank you all!

1


Reducing the amount of parameters

You can create an object to group all 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 an immense amount of parameters.

Signature of the method using an array:

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

Method signature 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 in not having to change the method signature if a new value arises.

Browser other questions tagged

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