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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?