duanmei2805 2011-02-11 13:33
浏览 83
已采纳

我怎么能用大量的isset检查和重复的生成参数来减少PHP代码?

Greetings. I am struggling to reduce a code segment that looks rather lengthy, leaving me unconvinced about its essentiality. It's a function to generate a multitude of session arrays used to fill out forms, and it has me verifying the existence of certain values in the argument array, with cases for every single one to generate the requested arrays. Here goes:

function prepOptional($formData) {

    $baseInfo=getBaseInfo();

    $_SESSION['fooData'] = (isset($formData['cbFoo']) ? prepBaseForm($baseInfo, 'foo',
            'Option foo') : '');

    $_SESSION['opt1Data'] = (isset($formData['cbOpt1']) ? prepBaseForm($baseInfo, 'opt1',
            'Option 1') : '');

    $_SESSION['barData'] = (isset($formData['cbBar']) ? prepBaseForm(prepOtherArray($formData), 'bar',
            'Option bar', 'Optional argument') : '');

}

The function accepts a formdata array as argument, and depending on the contained values it generates the corresponding arrays in session; this happens only after checking the existence of the matching flag, hence the issets and the ternary operator.

The called function, prepBaseForm, separates the appending of the second and third arguments, corresponding to a filename and a friendly name, to the first argument, which is an array; it also accepts an optional fourth parameter, indicating a file to concatenate to the filled form.

If the value is not present in the array, the code intentionally clears the corresponding session variable to '' in order to keep the generation order intact (to this end: is there a better way of doing this, considering that there are scenarios in which the given generation order will be broken?).

The actual code has about twenty generation blocks with this format, and it's getting quite lengthy; one optimization I thought of was to loop through the given fields and generalize the generation process, but as you can see, many times the arguments differ, or the input array for the prepBaseForm function has to be generated another way. Any ideas on how I could tackle this refactoring? Thanks in advance for your response.

  • 写回答

1条回答 默认 最新

  • doutuo7609 2011-02-11 14:20
    关注

    One option would be to provide defaults, and just run it if the values are changed using normal conditionals. Then you can clean up further by putting everything into a simple loop.:

    function prepOptional($formData) {
        $baseInfo=getBaseInfo();
    
        $options = array(
            'foo' => 'Option foo', 
            'opt1' => 'Option Opt1',
            'bar' => 'Option Bar',
        );
        foreach ($options as $name => $text) {
            //Add defaults for formData and Session arrays)
            $formData += array('cb' . ucfirst($name) => '');
            $_SESSION += array($name . 'Data' => '');
    
            if ($formData['cb' . ucfirst($name)]) {
                $_SESSION[$name . 'Data'] = prepBaseForm(
                    $baseInfo, 
                    $name, 
                    $text
                );
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题