NYCPHP Meetup

NYPHP.org

[nycphp-talk] Named parameters

Adam Maccabee Trachtenberg adam at trachtenberg.com
Fri May 9 18:32:30 EDT 2003


On Fri, 9 May 2003, Emmanuel. M. Decarie wrote:

> I miss named parameters for function in PHP.

[snip]

> Is there any technique that I'm missing here?

Not really, AFAIK you need to use an associative array to fake named
params in PHP.

However, if you use extract() at the top of your function, it makes
referencing the variables much easier (and better looking) inside the
function:

function namedParams ( $ar ) {
    extract ( $ar ); 

    echo "$param1 $param2";
}

extract() will import variables from an array into the current symbol
table. You can optionally decide whether they should overwrite
existing variables and also prefix their name with a constant.

See http://www.php.net/extract

-adam

PS: I notice this hint is missing from Recipe 6.4 (Using Named
Parameters) of the Cookbook. Oops. More material for the second
edition. :)

-- 
adam at trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!




More information about the talk mailing list