dqfkd82886 2014-09-15 09:39
浏览 15
已采纳

变量范围 - php - 使用另一个函数中的变量

I am having trouble using a variable generated in one function, as a variable in a second function.

The Problem:

I get Notice: Undefined variable: parameter in the validate function, on the line:

$this->$methodName($item,$value,$parameter) OR $valid=false;

When the function call for splitRulesAndParameters is simply replaced with the code within the function, the problem goes away.

The Scenario:

The following two functions are both within the Validator class, the first, validate, makes use of the second, splitRulesAndParameters

Here is the validate function:

public function validate($data, $rules)
{
    $valid= true;

    foreach($rules as $item=>$ruleSet)
    {
        $ruleSetArray=explode('|',$ruleSet);

        foreach($ruleSetArray as $rule)
        {
            $this->splitRulesAndParameters($rule);

            $methodName='validate'.ucfirst($rule);
            $value = isset($data[$item]) ? $data[$item] : NULL;

            if(method_exists($this, $methodName))
            {
                $this->$methodName($item,$value,$parameter) OR $valid=false;
            }
        }
    }

    return $valid;
}

And here is the splitRulesAndParameters function

public function splitRulesAndParameters($rule)
{
    $position = strpos($rule, ':');
    if($position !==false)
    {
        $parameter = substr($rule,$position + 1);
        $rule = substr($rule,0,$position);
    }
    else
    {
        $parameter='';
    }
}
  • 写回答

3条回答 默认 最新

  • dongmi4734 2014-09-15 09:47
    关注

    Seeing as the problem goes away if you "inline" the code in splitRulesAndParameters, I suspect the $parameters variable is used in that method. If so, simply have that method return the value of this variable, and assign it to a variable local to the validate method you've posted here:

    $parameters = $this->splitRulesAndParameters($rule);
    

    After adding this to the splitRulsAndParameters method:

    return $parameters;
    

    The method itself also modifies the $rule value. Again: this $rule variable is local to each method. It may have the same name, but the value is a copy. Any changes you make to $rule in splitRulesAndParameters is not reflected by $rule in your validate method. If I were you, I'd write:

    public function splitRulesAndParameters($rule)
    {
        $position = strpos($rule, ':');
        if($position !==false)
        {
            return array(
                'parameter' => substr($rule, $position+1),
                'rule'      => substr($rule, 0, $position)
            );
        }
        return array(
            'parameter' => null,//no param == null, IMO, change to '' if you want
            'rule'      => $rule
        );
    }
    

    Then, to change the variables in validate:

    $split = $this->splitRulesAndParameters($rule);
    $rule = $split['rule'];
    $parameter = $split['parameter'];
    

    That ought to do it.

    Side-note:
    You seem to be validating everything that needs validating, even if the first validation failed. If I were you, I'd change this fugly statement:

    $this->$methodName($item,$value,$parameter) OR $valid=false;
    

    To a more efficient:

    if (!$this->{$methodName}($item, $value, $parameter))
        return false;//if validation fails, return false
    

    That stops any further valiation from being executed: if one value is invalid, then just stop there. To continue is pointless, because the data-set is not entirely valid anyway.

    Bonus:
    Using a colon to separate the method name, and some parameter(s) does allow you to specify multiple params, too, and it allows you to simplify the splitRulesAndParameters some more:

    protected function splitRulesAndParameters($rule)
    {
        $all = explode(':', $rule);
        return array(
            'rule'   => array_shift($all),//removes first element in array
            'params' => $all//rest of the array
        );
    }
    

    Tweak this a little to better suite your needs

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大