Work around for assigning $_POST variables as parameter defaults in a function
<p>I'm working on a web-accessible API and have been trying to figure out the tidiest and most efficient way of assigning $_POST / $_GET variables as function parameters. I know variables can't be used as parameter defaults, so the goal is DRY (Don't Repeat Yourself); to have API functions usable from both within the application and through the external interface.</p>

<p><strong>Background:</strong></p>

<p><strong>1)</strong> Currently, API functions are organised into classes with public static methods. When a remote request is made, the requested class and method are (safely) determined, and if the class+method combination is valid, it is called using <code>call_user_func("$class::$method")</code>.</p>

<p><strong>2)</strong> The the code that ends up calling <code>class::method()</code> is intentionally dumb - <em>it doesn't know or care about the parameters a function takes</em>. As functions cannot be called with missing parameters, all functions in the API must either have no parameters, or no parameters without defaults. I figured the cleanest thing to do here was to have required parameters defined with a default of <code>null</code> and let functions handle the assignment of $_POST variables internally:</p>

<pre><code>public static function complete($id = null, $offset = 0, $limit = 50){ ... }
</code></pre>

<p><strong>3)</strong> Internally, API methods return regular PHP variables, then the external interface encodes this value with <code>json_encode()</code>.</p>

<p><strong>What I have now that works:</strong></p>

<p>I tried a few monstrosities at first that required 10 or more extra lines per function, but I've whittled it down to a reusable function. I had a look at <a href="http://php.net/manual/en/function.extract.php" rel="nofollow"><code>extract()</code></a> but it wasn't precise or safe enough for my purpose.</p>

<pre><code>function assign($postIndex, &amp;$target, $require = true){
    // Any code assigned parameter overrides remote values
    if($target != null &amp;&amp; $require == true)
        return;

    // Force parameter requirement if specified
    if($require &amp;&amp; !isset($_POST[$postIndex]))
        throw new MissingParameterException();

    // Assign to referenced variable if it's set
    if(isset($_POST[$postIndex]))
        $target = $_POST[$postIndex];
}
</code></pre>

<p><strong>Used like:</strong></p>

<pre><code>public static function delete($id = null){
    assign('id', $id);

    ...

}
</code></pre>

<p>Please note that <strong>I am aware of the potential injection vulnerability</strong>; all API functions clean their parameters regardless of origin. I am also aware that other people writing functions for an API using this method might assume parameters are safe, however as <code>assign()</code> has to be explicitly called, I think this is somewhat void (more so if the function was renamed  <code>assign_raw_post_var_to_param()</code>) - it's essentially as explicit as writing <code>$name = $_POST['name'];</code>.</p>

<p><strong>The question...</strong></p>

<p>Is there a better way to assign $_POST / $_GET variables to function parameters that allows functions to be called both through an external interface and from code transparently? How would you implement it if so?</p>
