Reputation: 4054
I have developped an habit of using a return to stop a code sequence from executing, even if the return is used on a function that returns nothing
$form = $this->getForm('activity');
$this->view->form = $form;
return $this->render('create'); // Like this
In Zend Framework it stop the execution of the current action and renders the view which name is the parameter of the function $this->render()
.
I have recently ran PHPMD on my code and all of those statements gets flagged as Void function result used
I was wondering, how bad is this habit and what would be better?
Upvotes: 3
Views: 583
Reputation: 7297
The only point I see is that $this->render('create')
returns void which should not be returned again.
If the action does not require any return value (Zend Framework 1), then you should place your return
on a separate line without any return value.
$form = $this->getForm('activity');
$this->view->form = $form;
$this->render('create'); // Like this
return;
This is common syntax in Zend Framework at least for forward
. You will also often see it for redirect
, but this one already has an exit-statement within the Zend code. According to the ZF1 documentation the intention of forward is to execute the current action and then also execute a second action. But in their code example, they also use return $this->_forward()
. On my job however, I learnt to put the return
onto a new line to make clear that nothing is returned.
As an alternative, you can of course use if else
blocks. However, in my opinion they are not always clearer (and I am not a fan of these strict Java rules).
if ($something) {
$this->_forward();
} else {
// do other code
}
Upvotes: 2