在PHP中模拟命名的函数参数,好的或坏的想法? [英] Emulating named function parameters in PHP, good or bad idea?

查看:98
本文介绍了在PHP中模拟命名的函数参数,好的或坏的想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命名的函数参数可以在PHP中模拟,如果我写这样的函数



$ p $函数pythonic(array $ kwargs)
{
extract($ kwargs);
// ..函数体的其余部分
}

//如果params是可选的或者需要默认值
函数pythonic(array $ kwargs = array( 'name'=>'Jon双向飞碟'))
{
extract($ kwargs);
// ..函数体的其余部分
}

除了输IDE中的intellisense这种方法的其他可能的缺点是什么?

编辑:

在这种情况下,安全问题不应该成为一个非问题,因为提取的变量仅限于函数作用域?解析方案

p>我建议使用关联数组传递命名参数,但将它们保留在数组中而不会提取它们。

  function myFunc (array $ args){
echoHi,。的$ args [名];
// etc
}

这有几个原因。看看这个函数,你可以清楚地看到我指的是传递给函数的参数之一。如果你提取它们,并且没有注意到 extract(),那么你(或者下一个人)会在那里挠挠你的脑袋,想知道这个 $ name 变量来自。即使你知道你正在将参数提取到局部变量,它仍然是一个猜测游戏。

其次,它确保其他代码不会覆盖参数。你可能写了你的函数,期望只有名为 $ foo $ bar 的参数,所以在其他代码中,例如,定义 $ baz = 8; 。之后,你可能想扩展你的函数来获取一个名为baz的新参数,但忘记改变你的其他变量,所以不管在参数中传递什么, $ baz 将始终设置为8。



使用数组也有一些好处(这些同样适用于提取或离开数组的方法):可以在每个函数的顶部设置一个名为 $ defaults 的变量:

  function myFunc(array $ args){
$ default = array(
name=>John Doe,
age=>30
);
//用参数覆盖所有的默认值
$ args = array_merge($ defaults,$ args);
//如果你想

echoName:,你可以在这里提取($ args)。 $ args ['name']。 ,年龄:。的$ args [年龄];
}

myFunc(array(age=> 25)); //姓名:John Doe,年龄:25

您甚至可以从 $ args ,它们没有对应的 $ default 值。这样你就知道你有哪些变量。


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?

解决方案

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.

这篇关于在PHP中模拟命名的函数参数,好的或坏的想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆