Retrieving user details from a custom form I have been working on a Magento module for sometime now (not because it is a large module, it's my first and I really struggled with it). I now have it working, which I was really pleased with at first, but now I would like to improve it by increasing the reusability of the code.

While making my module I have looked at other modules to learn from them and have seen that many actions are only a line or two and I have tried keeping in with the skinny controller approach but failed, my example is as follows:

I have a function that get a users details that they have inputted into a custom form.

protected function _setPostData()
{        
    $this-&gt;_salutation = $this-&gt;getRequest()-&gt;getPost('salutation');
    $this-&gt;_f_name = $this-&gt;getRequest()-&gt;getPost('f_name');
    $this-&gt;_l_name = $this-&gt;getRequest()-&gt;getPost('l_name');
    $this-&gt;_email = $this-&gt;getRequest()-&gt;getPost('email');
    $this-&gt;_emailAddressCheck = $this-&gt;getRequest()-&gt;getPost('emailAddressCheck');
    $this-&gt;_gender = $this-&gt;getRequest()-&gt;getPost('gender');
    $this-&gt;_country = $this-&gt;getRequest()-&gt;getPost('country');
    $this-&gt;_pref_lang = $this-&gt;getRequest()-&gt;getPost('pref_lang');        
}


I feel that this is not the correct way to do it and that there is a better way of achieving the same goal. As you can see, this function gets the posts data and assigns it to attributes that I've set at the start of the class. I have several other examples that are very similar to the above and if someone could please offer some guidance on this one I am sure I will be able to work out the others.

This example is held within the index action. Should I put it in a helper? Once it's created correctly, will there be a few occasions in which I will be able to use it again.
First, I would apply self-encapsulation by changing:

$this-&gt;_salutation = $this-&gt;getRequest()-&gt;getPost('salutation');


to:

$this-&gt;set_salutation( $this-&gt;get_parameter( "salutation" ) );


Since PHP can call functions using a string, it can be simplified further:

function set( $p ) {
  $f = '$this-&gt;set_' . $p;
  $f( $this-&gt;get_parameter( $p ) );
}

function get_parameter( $p ) {
  return $this-&gt;get_request()-&gt;get_post( $p );
}


Then:

$this-&gt;set( "salutation" );
$this-&gt;set( "first_name" );
$this-&gt;set( "last_name" );
$this-&gt;set( "email" );
// ... etc.


You can take that one step further by iterating over all HTML FORM variables, which removes the repetition altogether:

foreach( $this-&gt;get_form_parameters() as $param_name =&gt; $param_value ) {
  $this-&gt;set( $param_name );
}


However, that could lead to attempts on making arbitrary function calls by the client, so you'd have to be careful.

What I like about removing the repetition is that it sparks of being consistent with the DRY principle. The HTML form has the list of variables that need to be set -- now the developer need only change the HTML form variable list to change what variables are set by the object. This leads to a single source for the variable list.