Parameters are one thing and return is something else.
Imagine that each method represents a work to be done. The parameters are what is required for the work to be performed (in addition to what is already available through $this
). Return is the result that the method gives to the caller.
To illustrate this in a very simple and didactic way, imagine some problems:
You go to the bakery to buy bread. When you arrive at the bakery, here's what you need:
And here's what you get as a result:
In this way, let's assume that I have modeled the function comprarPaes
:
public function comprarPaes($numeroDePaes, $meioDePagamento) {
// ...
return $sacola;
}
That is, this function has two parameters and has a return.
You're going to take the garbage out. Here's what you need:
I could model this function more or less like this:
public function botarLixoPraFora($lixo, $lixeira) {
$lixo->embalar();
sairParaRua();
$lixeira->colocarLixo($lixo);
}
And notice that this function, despite having two parameters, has no return. It simply performs a task and once it is done, no special result is needed beyond the execution of the task itself.
The dollar is very unstable in the last few days and you want to know the quote today:
public function buscarCotacaoDoDolar() {
// ...
return $cotacao;
}
And this function, although it has no parameters, has a return type.
You'll turn on the radio to listen to music:
public function ouvirMusica() {
$radio = localizarRadio();
$radio->ligar();
$radio->sintonizarAondeTemUmaMusicaLegal();
}
And this function has no parameters and no return type. It consists of a task to be done where nothing needs to be returned, no special data (parameter) is required to execute the task, and no special result is needed beyond the execution of the task itself.
Finally, look at the four combinations we have, all perfectly valid:
- With parameters and with return.
- With parameters and no return.
- No parameters and return.
- No parameters and no return.
That is, there is no rule that says that if there is no return then it should not have parameters or the opposite.
When a function can not perform the task at hand, or fails to execute it, the appropriate mechanism to use is the handling of exceptions. For example:
public function comprarPaes($numeroDePaes, $meioDePagamento) {
$padaria = irAPadaria();
if (!$padaria->estaAberta()) {
throw new Exception("A padaria não está aberta. Não dá para comprar pão.");
}
$padaria->irAteOBalcao();
$atendente = $padaria->esperarAtendente();
$atendente->pedirPaes($numeroDePaes);
try {
$atendente->realizarPagamento($meioDePagamento);
} catch (Exception $e) {
// Ocorreu um erro com o pagamento.
// Pode ser que não tenha dinheiro suficiente
// ou o cartão pode estar fora da validade,
// ou algum outro problema desse tipo.
throw new Exception("Problema no pagamento: " . $e->getMessage());
}
$sacola = $atendente->receberSacola();
return $sacola;
}
In this case, note that at points where something goes wrong that makes it impossible for the task to be completed, an exception is thrown. It is not good to return true
if it worked and false
if it did not, as this tends to be confusing and the idea of exception handling came up exactly so that you do not have to do this sort of thing. In addition, the exception may carry much more information about the error than a simple true
or false
.
Note also that the function comprarPaes
has return and parameters, and within it is used realizarPagamento
that has parameter but has no return, esperarAtendente
that has return and has no parameters and irAteOBalcao
it has neither return nor parameters. Anyway, we can use the four possible combinations together with exception handling.