Search code examples
phpfunctionnamed-parameters

Emulating named function parameters in PHP, good or bad idea?


Named function parameters can be emulated in PHP if I write functions like this

function pythonic(array $kwargs)
{
    extract($kwargs);
    // .. rest of the function body
}

// if params are optional or default values are required
function pythonic(array $kwargs = array('name'=>'Jon skeet'))
{
    extract($kwargs);
    // .. rest of the function body
}

Apart from losing intellisense in IDEs what are the other possible downsides of this approach?

Edit:

Security: Shouldn't security be a non-issue in this case, as the extracted variables are limited to function scope?


Solution

  • I would suggest using the associative array to pass named parameters, but keep them in the array without extracting them.

    function myFunc(array $args) {
        echo "Hi, " . $args['name'];
        // etc
    }
    

    There's a couple of reasons for this. Looking at that function, you can quite clearly see that I'm referring to one of the arguments passed into the function. If you extract them, and don't notice the extract() you (or the next guy) will be there scratching your head wondering where this "$name" variable came from. Even if you do know you're extracting the arguments to local variables, it's still a guessing game to a certain degree.

    Secondly, it ensures that other code doesn't overwrite the args. You may have written your function expecting only to have arguments named $foo and $bar, so in your other code, you define $baz = 8;, for example. Later on, you might want to expand your function to take a new parameter called "baz" but forget to change your other variables, so no matter what gets passed in the arguments, $baz will always be set to 8.

    There are some benefits to using the array too (these apply equally to the methods of extracting or leaving in the array): you can set up a variable at the top of each function called $defaults:

    function myFunc (array $args) {
        $default = array(
            "name" => "John Doe",
            "age" => "30"
        );
        // overwrite all the defaults with the arguments
        $args = array_merge($defaults, $args);
        // you *could* extract($args) here if you want
    
        echo "Name: " . $args['name'] . ", Age: " . $args['age'];
    }
    
    myFunc(array("age" => 25)); // "Name: John Doe, Age: 25"
    

    You could even remove all items from $args which don't have a corresponding $default value. This way you know exactly which variables you have.