Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 192049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:23:36+00:00 2026-05-11T16:23:36+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:23:36+00:00Added an answer on May 11, 2026 at 4:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If an unhandled exception occurs, the only event that is… May 12, 2026 at 11:03 pm
  • Editorial Team
    Editorial Team added an answer The US doesn't conform to IBAN standards for account numbers;… May 12, 2026 at 11:03 pm
  • Editorial Team
    Editorial Team added an answer You can add a discriminator forumla to the primary class… May 12, 2026 at 11:03 pm

Related Questions

I read This article and i found it interesting. To sum it up for
I've been doing a project at work recently focused on an almost entirely client-driven
Currently I am making some decisions for my first objective-c API. Nothing big, just
Scheme uses a single namespace for all variables, regardless of whether they are bound

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.