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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序