功能变成无限循环

时间:2009-08-25 21:10:20

标签: php function

我在php中编写的函数有问题。如您所见,函数使用自身返回值的数组。

    public function getRepeat($day = "array")
{
    if ($day == 'array')
    {//Return an array with the repeated days as values
        foreach (array(1,2,3,4,5,6,0) as $value) 
        {
            if ($this->getRepeat($value))
            {
                $returnArray[] = $value;
            }
        }
        return $returnArray;
    }
    else if (in_array($day, array(1,2,3,4,5,6,0) ))
    {
        if ($day == 1)
            return $this->repeat1;
        if ($day == 2)
            return $this->repeat2;
        if ($day == 3)
            return $this->repeat3;
        if ($day == 4)
            return $this->repeat4;
        if ($day == 5)
            return $this->repeat5;
        if ($day == 6)
            return $this->repeat6;
        if ($day == 0)
            return $this->repeat0;
    }
}

一旦它自己调用每个变量就会变成无限循环。

是什么导致这种情况?

3 个答案:

答案 0 :(得分:5)

您必须始终考虑分两部分编写递归函数:

  1. 基本情况 - 此时您停止递归并返回一个值(即列表为空)
  2. 递归情况 - 如何再次调用函数以及输入与前一次调用的不同之处(即您是否发送了列表的尾部)
  3. 确保这两个规则保持应该导致递归函数在输入有效的情况下终止。

    这是一个递归解决方案 - 但是它在Java中:)

        public static void main(String[] args) {
    
        List<Integer> testVals = new ArrayList<Integer>();
        testVals.add(0);
        testVals.add(1);
        testVals.add(2);
        testVals.add(3);
        testVals.add(4);
        testVals.add(5);
    
        List<Integer> toMatch = new ArrayList<Integer>(testVals);
    
        List<Integer> matches = new ArrayList<Integer>();
    
        repeatRec(testVals, matches, toMatch);
    
        System.out.println("Matches " + matches);
    }
    
    public static void repeatRec(List<Integer> toTest, List<Integer> matches, List<Integer> toMatch) {
    
    
        if (toTest.isEmpty()) {
            //we are done
            return;
        } else {
    
            Integer head = toTest.get(0);
    
            if (toMatch.contains(head)) {
                matches.add(head);
    
            }
    
            //could have else here if we're only interested in the first match
            repeatRec(toTest.subList(1, toTest.size()), matches, toMatch);
        }
    }
    

答案 1 :(得分:3)

当你想到它时,它很简单。

0 == 'any text which does not start with a number'

您的最后一位数字0将导致无限循环。所以你需要把它改成

if ($day === 'array')

修改

我也冒昧地修改你的代码:

/**
 * @obsolete
 */
public function getRepeat($day = "array")
{
    if ($day === 'array') {
     return $this->getAllRepeat();
}
    return $this->getRepeatByDay($day);

}

public function __construct()
{
    $this->repeat = array_fill(0, 7, '');
}

public function getAllRepeat()
{
    return $this->repeat;
}

public function __get($value) {
    switch ($value) {
        case 'repeat0':
        case 'repeat1':
        case 'repeat2':
        case 'repeat3':
        case 'repeat4':
        case 'repeat5':
        case 'repeat6':
            return $this->getRepeatByDay(intval(substr($value, -1, 1)));
    }
}

public function getRepeatByDay($day)
{
    if (!isset($this->repeat[$day])) {
        return null;
    }
    return $this->repeat[$day];
}

答案 2 :(得分:0)

我能否提出一个更好的解决方案:

public function getRepeat($day = "array")
{
    foreach (array(1,2,3,4,5,6,0) as $value) 
    {
        $tmp = "repeat".$value;
        if ($this->$tmp)
        {
            $returnArray[] = $value;
        }
    }
    return $returnArray;
}

至于为什么你的功能没有结束,我不确定。通常情况下,我会尝试使用两个单独的函数调用,例如:

public function getRepeat()
{
                foreach (array(1,2,3,4,5,6,0) as $value) 
                {
                        if ($this->getRepeat_r($value))
                        {
                                $returnArray[] = $value;
                        }
                }
                return $returnArray;
}
private function getRepeat_r($day)
{
        if (in_array($day, array(1,2,3,4,5,6,0) ))
        {
                if ($day == 1)
                        return $this->repeat1;
                if ($day == 2)
                        return $this->repeat2;
                if ($day == 3)
                        return $this->repeat3;
                if ($day == 4)
                        return $this->repeat4;
                if ($day == 5)
                        return $this->repeat5;
                if ($day == 6)
                        return $this->repeat6;
                if ($day == 0)
                        return $this->repeat0;
        }
}

这使得事情更易于阅读,而且更加稳定,只是当PHP不应该将其解释为"array"时。