0

Here's the situation; below is a piece of PHP code which is frequently reused.

if (! isset($_REQUEST['process_form'])
{
   // render form
   echo "<form>";
   // snipped

   // important bit! Remember which id we are processing
   echo "<input hidden='id' value='$id'>";

   // snipped
} else {
  // process the form
}

I wish to encapsulate this into a function, akin to

  class ProcessForm() {
   function execute(array $request, $id) { };
  }

The issue here is; the $id parameter is only needed when rendering the form. When processing the form after a user input or through an AJAX handler, I don't need the $id at all.

How could I refactor to get rid of the optional variable $id?

Extrakun
  • 19,057
  • 21
  • 82
  • 129
  • *(related)* [What's wrong with using `$_REQUEST`](http://stackoverflow.com/questions/2142497/whats-wrong-with-using-request) – Gordon Oct 06 '10 at 08:58
  • Hence which is why I am refactoring. The execute function of ProcessForm takes in a generic array, not necessary $_REQUEST – Extrakun Oct 06 '10 at 16:09

1 Answers1

0

Optional parameters in PHP works like so

function example($id = NULL)
{
    if(is_null($id))
        echo '$id was omitted';
}
Kristoffer Sall-Storgaard
  • 10,576
  • 5
  • 36
  • 46