PHP:使用数组键识别函数参数 [英] PHP: Using array keys to identify function arguments

查看:115
本文介绍了PHP:使用数组键识别函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用户定义了许多函数,其中一些具有六个,十个甚至更多的参数.当我忘记一个函数的参数是什么或它们的顺序时,阅读代码变得很困难.我设计了一种方法来解决这个问题,用单个数组替换所有参数,并使用数组键作为标签每个论点.因此,例如,代替

I user-define many functions and a few of them have six, ten, or even more arguments. Reading my code gets difficult when I forget what the arguments of a function are, or what order they go in. I've devised a way to deal with this, replacing all the arguments with a single array, and using array keys as labels for each argument. So, for example, instead of

function MyFunction(string $sSayThis, int $nRepeatTimes, bool $bLoud = $false) {...}

我现在有

function MyFunction(array $args)
   {$sSayThis = $args['sSayThis']); CheckType($sSayThis, 'string');
    $nRepeatTimes = $args['nRepeatTimes']); CheckType($nRepeatTimes, 'int');
    $bLoud = (IsSet($args['bLoud']) ? $args['bLoud'] : false); CheckType($bLoud, 'bool');
    ...
    }

对此函数的调用,而不是

A call to this function, instead of

MyFunction('Hello', 3, true);

现在看起来

MyFunction(array('sSayThis' => 'Hello', 'nRepeatTimes' => 3, 'bLoud' => true));

在只有三个参数的情况下(如本例所示)几乎没有必要,但是在读取具有六个或十个参数的函数的代码时可能会非常有帮助!同样,如果我只需要为第十个参数传递一个值并在此之前为所有可选参数使用默认值,则可以从调用中忽略那些其他参数,而不必为它们传递一系列的, ''.

This is hardly necessary when there are only three arguments, as in this example, but it could be very helpful when reading code for a function with six or ten arguments! Also, if I only need to pass a value for the tenth argument and use the default values for all the optional arguments before that, I can just omit those other arguments from the call instead of passing a series of , '' for them.

这是一个hack,看起来有点丑陋.但这确实有助于使我的代码自我记录并更易于阅读.

This is a hack, and it seems kind-of ugly. But it does help make my code self-documenting and easier to read.

我知道有一些IDE可以为我提供参数提示,但是我使用的是Notepad ++,但并没有做到这一点.

I know there are IDEs that would give me argument hints, but I'm using Notepad++, which doesn't do that.

在去年提出的类似问题中讨论了这个想法, PHP函数参数-是否使用数组?,但是该问题并未显示函数调用的样子,这是问题中最重要的部分.回答这个问题的一些人说,一个函数永远不需要十个参数,而有那么多参数则表明设计不佳.我了解这种担心,但有时算法只需要大量信息即可.

This idea is discussed in a similar question asked last year, PHP Function Arguments - Use an array or not?, but that question doesn't show what the function calls look like, which is the most important part of the question. Some people in answer to that question said that a function should never need ten arguments and having that many indicates poor design. I understand that concern, but sometimes an algorithm just needs a lot of information.

这种方法有什么问题吗,还是有更好的方法来自我记录这些函数调用?

Is there anything wrong with this approach, or is there a better way to self-document these function calls?

推荐答案

恕我直言,在代码可读性方面几乎没有任何区别.但是,第二种方法增加了一些新的缺点:

IMHO, there's hardly any difference in terms of code readability. The second approach, however, adds some new drawbacks:

  • 它不再受益于PHP类型提示
  • 您的IDE不再可以使用从代码和注释中解析的信息来提供有用的提示或自动完成

带有大量参数的函数通常是对遗留代码的一种指示,该遗留代码已超出其设计限制.我认为这需要进行一些重构,例如:

Functions with large number of arguments are normally an indicator of legacy code that has grown beyond its design limits. I think that calls for some refactoring, e.g.:

class Speaker
{
    /**
     * @var string
     */
    private $sayThis;

    /**
     * @var int
     */
    private $repeatTimes;

    /**
     * @var bool
     */
    private $loud;

    /**
     * @param string $sayThis
     */
    public function __construct(string $sayThis)
    {
        $this->sayThis = $sayThis;
    }

    public function times(int $repeatTimes)
    {
        $this->repeatTimes = $repeatTimes;
        return $this;
    }

    public function loud(bool $loud = false)
    {
        $this->loud = $loud;
        return $this;
    }

    public function say()
    {
        $output = str_repeat($this->sayThis, $this->repeatTimes);
        echo $this->loud
            ? mb_strtoupper($output)
            : $output;
    }
}

(new Speaker('Foo'))
    ->times(4)
    ->loud(true)
    ->say();

如您所见,我还摆脱了匈牙利的表示法.

As you can see, I also got rid of Hungarian notation.

这篇关于PHP:使用数组键识别函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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